Giter VIP home page Giter VIP logo

Comments (14)

shabbyrobe avatar shabbyrobe commented on June 28, 2024

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.

keleshev avatar keleshev commented on June 28, 2024

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.

shabbyrobe avatar shabbyrobe commented on June 28, 2024

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.

keleshev avatar keleshev commented on June 28, 2024

Maybe something like:

"""Usage: python -v prog.py <this> <that>"""

print docopt(__doc__, program='python -v prog.py')

WYSIWYG, but not DRY

from docopt.

EvanED avatar EvanED commented on June 28, 2024

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.

encukou avatar encukou commented on June 28, 2024

Or:

"""Usage: python -v prog.py <this> <that>"""

print docopt(__doc__, program='prog.py')

WYSIWYG and almost DRY

from docopt.

keleshev avatar keleshev commented on June 28, 2024

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.

njsmith avatar njsmith commented on June 28, 2024

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.

keleshev avatar keleshev commented on June 28, 2024

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.

keleshev avatar keleshev commented on June 28, 2024

We just need to decide on the keyword name. program? POSIX calls that utility_name, so maybe that?

from docopt.

voieducode avatar voieducode commented on June 28, 2024

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.

Darthfett avatar Darthfett commented on June 28, 2024

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.

keleshev avatar keleshev commented on June 28, 2024

@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 )":

def formal_usage(section):

Another hit: base your work on 0.6.x branch, not master.

from docopt.

 avatar commented on June 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.