Comments (14)
I think this is needed.
What about adding a parameter to docopt that gets run through python's "{yep}".format(**tokens)
method in the extras
function prior to output?
You could even add the version value to the tokens prior to doing the format:
"""
Thingy thing v{version}
Usage: {program} --this --that --the-other
-h, --help Help
--quack Escaped bracketage {{yep}}
"""
import docopt
if __name__ == "__main__":
args = docopt.docopt(__doc__, version="1.2.3",
tokens={'program':'python -v %s' % __file__})
It would be trivial to implement the simplest case of key/value substitution in the PHP version (though not some of the more complex stuff that python's format
method offers like nested attribute access and the like).
If this portability is a concern, you could do something like this instead of a format()
:
for token, sub in {'{'+k+'}': v for k, v in tokens.items()}.items():
doc = doc.replace(token, sub)
from docopt.
This proposition is sound; also with respect for python versions that don't support format()
. But this breaks WYSIWYG, and gives very little in return.
You can now do docopt(__doc__.format(...))
so you proposition has redundant functionality (in addition to solving the issue).
from docopt.
If you don't like the idea of adding the extra layer of token substitution, what about just replacing {program}, like so:
"""
Thingy thing v1.2.3
Usage: {program} --this --that --the-other
-h, --help Help
"""
import docopt
if __name__ == "__main__":
args = docopt.docopt(__doc__, program='python -v %s' % __file__)
I only added the extra token substitution because I thought "well, it's gotta do something like this in the code anyway, so why not open it up?". It's not essential.
If you want it to support multi-word program names like python -v program.py
, it either has to be able to find where the program name ends and the docopt stuff begins (not really possible with python -v program.py --docopt --stuff --here
, or substitute it in when the help is printed and no sooner so it doesn't get picked up by the parser. The proposed fix is targeted at the latter.
from docopt.
Maybe something like:
"""Usage: python -v prog.py <this> <that>"""
print docopt(__doc__, program='python -v prog.py')
WYSIWYG, but not DRY
from docopt.
I'm just skimming through some issues... what about making it possible to delimit the "program" with {...}
or something like that:
"""Usage: {python -v prog.py} <this> <that>"""
print docopt(__doc__)
It's DRY and almost WYSIWYG...
from docopt.
Or:
"""Usage: python -v prog.py <this> <that>"""
print docopt(__doc__, program='prog.py')
WYSIWYG and almost DRY
from docopt.
The following is most appealing to me.
"""Usage: python -v prog.py <this> <that>"""
print docopt(__doc__, program='python -v prog.py')
But I close this, because it doesn't seem that anyone missed that in 40 years.
from docopt.
Oh, too bad, I was just trying to figure out if I could switch to docopt for a project but this rules it out :-/
(I'm using the python -mfoo
system to easily distribute a bunch of small ad hoc utilities along with my package.)
from docopt.
Ok, after some thought and #262 I'm reconsidering this. One reason is that I'm working on a PEG grammar for docopt, and program name could not really be parsed with PEG without knowing it in advance. It could be extracted with a regex, but providing it as program=
smoothes it out (falling back to regex as a default).
from docopt.
We just need to decide on the keyword name. program
? POSIX calls that utility_name
, so maybe that?
from docopt.
I like "program" because it sounds more natural.
—
Sent from Mailbox
On Tue, Feb 24, 2015 at 11:20 AM, Vladimir Keleshev
[email protected] wrote:
Ok, after some thought and #262 I'm reconsidering this. One reason is that I'm working on a PEG grammar for docopt, and program name could not really be parsed with PEG without knowing it in advance. It could be extracted with a regex, but providing it as
program=
smoothes it out (falling back to regex as a default).Reply to this email directly or view it on GitHub:
#41 (comment)
from docopt.
Without this problem fixed, it limits the ability to use docopt for samples that run with your library.
For example:
GenLib
__init__.py
genlib/
__init__.py
genlib.py
samples/
__init__.py
S1/
__init__.py
s1.py
And then s1.py:
"""Sample 1 for Generic Library
Usage: python -m GenLib.samples.S1.s1
"""
from docopt import docopt
from ... import genlib
if __name__ == '__main__':
args = docopt(__doc__)
And then making the call like in the usage:
$ python -m GenLib.samples.S1.s1
This fails and docopt prints the usage information.
from docopt.
@Darthfett if you or someone else can submit a pull-request, I'll merge it. Hint: you need to change how formal_usage
function converts "usage: prog foo bar prog baz"
into "( foo bar ) | ( baz )"
:
Line 470 in 463d780
Another hit: base your work on 0.6.x
branch, not master.
from docopt.
Just had the problem as well, I tried to launch a module with python -m module
but Docopt kept displaying the help text. Would love to see this fixed.
from docopt.
Related Issues (20)
- Is docopt / docopt itself still being maintained? HOT 1
- Please upload a wheel package to pypi HOT 6
- get_docopts.sh fails on M1 Monterey 12.6
- Short options in docopt not returning correct values HOT 1
- [FeatureRequest] Allow hash comments in docstring ? HOT 4
- Make new release HOT 3
- try.docopt.org is offline HOT 1
- Dart port? HOT 1
- Make docopt exceptions public
- How to call "python -m mypackage [options]" HOT 1
- Is there some docopt validator? HOT 1
- Why cannot docopt parse args? HOT 3
- Please share wheel file on pypi HOT 1
- Docopt AssertionError
- Having same issues as #516
- Unix-style vs. Windows-style options HOT 2
- Abandonware status of docopt HOT 7
- Why is the docopt website not served through HTTPS?
- Any good reason not to use GitHub releases? HOT 1
- how to implement number argument? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docopt.