Giter VIP home page Giter VIP logo

Comments (7)

keleshev avatar keleshev commented on July 30, 2024

what about exit(__doc__)?

from docopt.

andreypopp avatar andreypopp commented on July 30, 2024

exit(__doc__.split("\n")[0]) — I want to print just usage line

from docopt.

keleshev avatar keleshev commented on July 30, 2024

your solution is not bad :-)

I'm now working in a pattern-matching branch, and there I extract the usage, and have it internally. I think it's a good idea to expose it. What API for that can you think of?

I have a weird idea of adding usage as a function attribute:

# 1
from docopt import docopt
exit(docopt.usage)  # or maybe __usage__

or maybe:

# 2
from docopt import docopt
docopt.print_usage()  # something similar to optparse? but weird since it's function attribute.

or:

# 3
from docopt import docopt, print_usage
print_usage()  # but this would require some global state

or make docopt a class:

# 4
from docopt import OptionParser
parser = OptionParser(__doc__)
options, arguments = parser.parse(sys.argv[1:])
parser.print_usage()  # very similar to optparse

from docopt.

andreypopp avatar andreypopp commented on July 30, 2024

I'm thinking of two possibilities:

# 1 (similar to your's # 4)
from docopt import OptionParser
parser = OptionParser(__doc__)
options, arguments = parser.parse(sys.argv[1:])
print >> sys.stderr, parser.usage # I think it's better to leave it to user how to print

or stick with function:

# 2
from docopt import docopt
options, args, meta = docopt(__doc__, need_meta=True) # or just return meta by default
print >> sys.stderr, meta.usage

from docopt.

keleshev avatar keleshev commented on July 30, 2024

I, as a user, would prefer:

from docopt import docopt
exit(docopt.usage)

Shortest variant, but weird.

I was thinking that maybe there will be no need to manually print usage after pattern-matching is implemented?
In that case whenever the invocation doesn't match the usage-pattern, docopt will exit showing usage. And since usage-patterns could be as complex as you need, there might be no need for printing usage manually?

from docopt.

andreypopp avatar andreypopp commented on July 30, 2024

I like the last paragraph — good point, looking forward for pattern-matching branch to be released.

from docopt.

keleshev avatar keleshev commented on July 30, 2024

@andreypopp
By the way, pattern-matching branch is somewhat usable now. If you want to try it out, you can take a look at this example: https://github.com/halst/docopt/blob/pattern-matching/test_example.py

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.