Coder Social home page Coder Social logo

cant pass None as argv about docopt HOT 13 CLOSED

docopt avatar docopt commented on June 2, 2024
cant pass None as argv

from docopt.

Comments (13)

keleshev avatar keleshev commented on June 2, 2024

from www.docopt.org:

argv is optional argument vector; by default it is the argument vector passed to your program (sys.argv[1:]). You can > supply it with list of strings (similar to sys.argv) e.g. ['--verbose', '-o', 'hai.txt'].

or you can pass a string, it will be .split()ed into list.

from docopt.

RonnyPfannschmidt avatar RonnyPfannschmidt commented on June 2, 2024

which means i cannot wrap docopt into a own function without jumping hops to deal with it

from docopt.

keleshev avatar keleshev commented on June 2, 2024

In you case I recommend set argv='' or argv=[]:

def main(argv=[]):
    docopt(__doc__, argv=argv) 

from docopt.

RonnyPfannschmidt avatar RonnyPfannschmidt commented on June 2, 2024

my function is supposed to use sys.argv or the default, docopt is the first argument parser i tried that doesn't allow that by just passing on None

from docopt.

keleshev avatar keleshev commented on June 2, 2024

Could you elaborate on that? docopt uses sys.argv by default if you don't pass anything.

from docopt.

mikeboers avatar mikeboers commented on June 2, 2024

I believe he would like to pass None to signal that docopt should use sys.argv.

from docopt.

keleshev avatar keleshev commented on June 2, 2024

I never used a wrapper function for that—could either of you make an example? I'm consider changing this behavior.

from docopt.

RonnyPfannschmidt avatar RonnyPfannschmidt commented on June 2, 2024

i usually have a main function as application entrypoint that takes argv, but can be none
my current chunk of code i just half-changed to docopt is :

#!python
from . import cli
def main(argv=None, composer=None):
    args = docopt(cli.__doc__, argv=argv or sys.argv[1:])
    print args
    composer = composer or Composer(py.path.local(args['--path']))
    composer.run_actions(actions)
    #XXX ew
    args.func(args, composer)

from docopt.

keleshev avatar keleshev commented on June 2, 2024

Is that for better testability of main?

I usually do:

"""Usage: ... """"
from docopt import docopt

main(args):
    code...

if __name__ == '__main__':
    try:
        main(docopt(__doc__))
    except KeyboardInterrupt:
        pass

from docopt.

RonnyPfannschmidt avatar RonnyPfannschmidt commented on June 2, 2024

its the entrypoint for a setuptools console script, and halfway for testability
i suppose i can shuffle it around a bit as needed

from docopt.

keleshev avatar keleshev commented on June 2, 2024

How about:

def main(argv=sys.argv[1:], composer=None):
    ...

Or do you still think docopt should handle None?

from docopt.

RonnyPfannschmidt avatar RonnyPfannschmidt commented on June 2, 2024

i suffeled things around a bit to be like this:

#!pythonimport py
import sys
from couchdb_compose import cli
from couchdb_compose.composer import Composer, actions
from docopt import docopt

def doc_main(args, composer=None):
    print args
    composer = composer or Composer(py.path.local(args['--path']))
    composer.run_actions(actions)
    cli._dispatch(args, composer)

def main(argv=None, composer=None):
    if argv is None:
        argv = sys.argv[1:]
    args = docopt(cli.__doc__, argv=argv)
    return doc_main(args, composer)

if __name__ == '__main__':
    main()

from docopt.

keleshev avatar keleshev commented on June 2, 2024

Looks good. Also the argv=argv or sys.argv[1:] trick.

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.