def test_issue_40(): with raises(SystemExit): # i.e. shows help docopt("usage: prog --help-commands | --help", "--help") assert docopt("usage: prog --aabb | --aa", "--aa") == { "--aabb": False, "--aa": True, }
def test_issue_65_evaluate_argv_when_called_not_when_imported(): import sys sys.argv = "prog -a".split() assert docopt("usage: prog [-ab]") == {"-a": True, "-b": False} sys.argv = "prog -b".split() assert docopt("usage: prog [-ab]") == {"-a": False, "-b": True}
def test_issue_71_double_dash_is_not_a_valid_option_argument(): with raises(DocoptExit): docopt("usage: prog [--log=LEVEL] [--] <args>...", "--log -- 1 2") with raises(DocoptExit): docopt( """usage: prog [-l LEVEL] [--] <args>... options: -l LEVEL""", "-l -- 1 2", )
def test_allow_double_dash(): assert docopt("usage: prog [-o] [--] <arg>\nkptions: -o", "-- -o") == { "-o": False, "<arg>": "-o", "--": True, } assert docopt("usage: prog [-o] [--] <arg>\nkptions: -o", "-o 1") == { "-o": True, "<arg>": "1", "--": False, } with raises(DocoptExit): # "--" is not allowed; FIXME? docopt("usage: prog [-o] <arg>\noptions:-o", "-- -o")
def test_count_multiple_flags(): assert docopt("usage: prog [-v]", "-v") == {"-v": True} assert docopt("usage: prog [-vv]", "") == {"-v": 0} assert docopt("usage: prog [-vv]", "-v") == {"-v": 1} assert docopt("usage: prog [-vv]", "-vv") == {"-v": 2} with raises(DocoptExit): docopt("usage: prog [-vv]", "-vvv") assert docopt("usage: prog [-v | -vv | -vvv]", "-vvv") == {"-v": 3} assert docopt("usage: prog -v...", "-vvvvvv") == {"-v": 6} assert docopt("usage: prog [--ver --ver]", "--ver --ver") == {"--ver": 2}
def test_options_first(): assert docopt("usage: prog [--opt] [<args>...]", "--opt this that") == { "--opt": True, "<args>": ["this", "that"], } assert docopt("usage: prog [--opt] [<args>...]", "this that --opt") == { "--opt": True, "<args>": ["this", "that"], } assert docopt("usage: prog [--opt] [<args>...]", "this that --opt", options_first=True) == { "--opt": False, "<args>": ["this", "that", "--opt"], }
def test_choices(): assert docopt( "Usage: prog [--foo=<foo>]\n" "Options: -f --foo=<foo> Lorem ipsum dolor [choices: A B C]", "--foo=A", ) == { "--foo": "A" } with raises(ValueError): docopt( "Usage: prog [--foo=<foo>]\n" "Options: -f --foo=<foo> Lorem ipsum dolor [choices: A B C]", "--foo=D", )
def test_docopt(): doc = """Usage: prog [-v] A Options: -v Be verbose.""" assert docopt(doc, "arg") == {"-v": False, "A": "arg"} assert docopt(doc, "-v arg") == {"-v": True, "A": "arg"} doc = """Usage: prog [-vqr] [FILE] prog INPUT OUTPUT prog --help Options: -v print status messages -q report only file names -r show all occurrences of the same error --help """ a = docopt(doc, "-v file.py") assert a == { "-v": True, "-q": False, "-r": False, "--help": False, "FILE": "file.py", "INPUT": None, "OUTPUT": None, } a = docopt(doc, "-v") assert a == { "-v": True, "-q": False, "-r": False, "--help": False, "FILE": None, "INPUT": None, "OUTPUT": None, } with raises(DocoptExit): # does not match docopt(doc, "-v input.py output.py") with raises(DocoptExit): docopt(doc, "--fake") with raises(SystemExit): docopt(doc, "--hel")
def test_default_value_for_positional_arguments(): doc = """Usage: prog [--data=<data>...]\n Options:\n\t-d --data=<arg> Input data [default: x] """ a = docopt(doc, "") assert a == {"--data": ["x"]} doc = """Usage: prog [--data=<data>...]\n Options:\n\t-d --data=<arg> Input data [default: x y] """ a = docopt(doc, "") assert a == {"--data": ["x", "y"]} doc = """Usage: prog [--data=<data>...]\n Options:\n\t-d --data=<arg> Input data [default: x y] """ a = docopt(doc, "--data=this") assert a == {"--data": ["this"]}
def test_commands(): assert docopt("Usage: prog add", "add") == {"add": True} assert docopt("Usage: prog [add]", "") == {"add": False} assert docopt("Usage: prog [add]", "add") == {"add": True} assert docopt("Usage: prog (add|rm)", "add") == {"add": True, "rm": False} assert docopt("Usage: prog (add|rm)", "rm") == {"add": False, "rm": True} assert docopt("Usage: prog a b", "a b") == {"a": True, "b": True} with raises(DocoptExit): docopt("Usage: prog a b", "b a")
def test_issue34_unicode_strings(): try: assert docopt(eval("u'usage: prog [-o <a>]'"), "") == { "-o": False, "<a>": None } except SyntaxError: pass # Python 3
def test_issue_68_options_shortcut_does_not_include_options_in_usage_pattern(): args = docopt("usage: prog [-ab] [options]\n" "options: -x\n -y", "-ax") # Need to use `is` (not `==`) since we want to make sure # that they are not 1/0, but strictly True/False: assert args["-a"] is True assert args["-b"] is False assert args["-x"] is True assert args["-y"] is False
def runtest(self): try: result = type_docopt.docopt(self.doc, argv=self.argv) except type_docopt.DocoptExit: result = "user-error" if self.expect != result: raise DocoptTestException(self, result)
def test_short_options_error_handling(): with raises(DocoptLanguageError): docopt("Usage: prog -x\nOptions: -x this\n -x that") with raises(DocoptExit): docopt("Usage: prog", "-x") with raises(DocoptLanguageError): docopt("Usage: prog -o\nOptions: -o ARG") with raises(DocoptExit): docopt("Usage: prog -o ARG\nOptions: -o ARG", "-o")
def test_custom_types(): class CustomType: def __init__(self, number): self.number = int(number) if self.number >= 10: raise ValueError def __eq__(self, other): return self.number == other.number assert docopt( "Usage: prog [--foo=<foo>]\n" "Options: -f --foo=<foo> Lorem ipsum dolor [type: CustomType]", argv="--foo=1", types={"CustomType": CustomType}, ) == { "--foo": CustomType("1") } assert docopt( "Usage: prog [--foo=<foo>]\n" "Options: -f --foo=<foo> Lorem ipsum dolor [type: CustomType]", argv="", types={"CustomType": CustomType}, ) == { "--foo": None } with raises(ValueError): docopt( "Usage: prog [--foo=<foo>]\n" "Options: -f --foo=<foo> Lorem ipsum dolor [type: CustomType]", argv="--foo=20", types={"CustomType": CustomType}, ) assert docopt( "Usage: prog [--foo=<foo>]\n" "Options: -f --foo=<foo> Lorem ipsum dolor [type: CustomType] [default: 5]", "", types={"CustomType": CustomType}, ) == { "--foo": CustomType("5") }
def test_any_options_parameter(): with raises(DocoptExit): docopt("usage: prog [options]", "-foo --bar --spam=eggs") # assert docopt('usage: prog [options]', '-foo --bar --spam=eggs', # any_options=True) == {'-f': True, '-o': 2, # '--bar': True, '--spam': 'eggs'} with raises(DocoptExit): docopt("usage: prog [options]", "--foo --bar --bar") # assert docopt('usage: prog [options]', '--foo --bar --bar', # any_options=True) == {'--foo': True, '--bar': 2} with raises(DocoptExit): docopt("usage: prog [options]", "--bar --bar --bar -ffff") # assert docopt('usage: prog [options]', '--bar --bar --bar -ffff', # any_options=True) == {'--bar': 3, '-f': 4} with raises(DocoptExit): docopt("usage: prog [options]", "--long=arg --long=another")
def fn(self, arg): try: opt = docopt(fn.__doc__, arg) except DocoptExit as e: # The DocoptExit is thrown when the args do not match. # We print a message to the user and the usage block. print("Invalid Command!") print(e) return except SystemExit: # The SystemExit exception prints the usage for --help # We do not need to do the print here. return return func(self, opt)
"""Usage: arguments_example.py [-vqrh] [FILE] ... arguments_example.py (--left | --right) CORRECTION FILE Process FILE and optionally apply correction to either left-hand side or right-hand side. Arguments: FILE optional input file CORRECTION correction angle, needs FILE, --left or --right to be present Options: -h --help -v verbose mode -q quiet mode -r make report --left use left-hand side --right use right-hand side """ from type_docopt import docopt if __name__ == "__main__": arguments = docopt(__doc__) print(arguments)
@docopt_cmd def do_tcp(self, arg): """Usage: tcp <host> <port> [--timeout=<seconds>]""" print(arg) @docopt_cmd def do_serial(self, arg): """Usage: serial <port> [--baud=<n>] [--timeout=<seconds>] Options: --baud=<n> Baudrate [default: 9600] """ print(arg) def do_quit(self, arg): """Quits out of Interactive Mode.""" print("Good Bye!") exit() opt = docopt(__doc__, sys.argv[1:]) if opt["--interactive"]: MyInteractive().cmdloop() print(opt)
Available commands: train predict evaluate test Options: -h --help Show this. See 'python main.py <command> --help' for more information on a specific command. """ from pathlib import Path from type_docopt import docopt if __name__ == "__main__": args = docopt(__doc__, options_first=True) argv = [args["<command>"]] + args["<args>"] if args["<command>"] == "train": from train import __doc__, train train(docopt(__doc__, argv=argv, types={"path": Path})) elif args["<command>"] == "predict": from infer import __doc__, infer infer(docopt(__doc__, argv=argv, types={"path": Path})) else: raise NotImplementedError( f"Command does not exist: {args['<command>']}")
"""usage: git add [options] [--] [<filepattern>...] -h, --help -n, --dry-run dry run -v, --verbose be verbose -i, --interactive interactive picking -p, --patch select hunks interactively -e, --edit edit current diff and apply -f, --force allow adding otherwise ignored files -u, --update update tracked files -N, --intent-to-add record only the fact that the path will be added later -A, --all add all, noticing removal of tracked files --refresh don't add, only refresh the index --ignore-errors just skip files which cannot be added because of errors --ignore-missing check if - even missing - files are ignored in dry run """ from type_docopt import docopt if __name__ == "__main__": print(docopt(__doc__))
checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository push Update remote refs along with associated objects remote Manage set of tracked repositories See 'git help <command>' for more information on a specific command. """ from subprocess import call from type_docopt import docopt if __name__ == "__main__": args = docopt(__doc__, version="git version 1.7.4.4", options_first=True) print("global arguments:") print(args) print("command arguments:") argv = [args["<command>"]] + args["<args>"] if args["<command>"] == "add": # In case subcommand is implemented as python module: import git_add print(docopt(git_add.__doc__, argv=argv)) elif args["<command>"] == "branch": # In case subcommand is a script in some other programming language: exit(call(["python", "git_branch.py"] + argv)) elif args["<command>"] in "checkout clone commit push remote".split(): # For the rest we'll just keep DRY:
Arguments: PATH destination path Options: -h --help show this help message and exit --version show version and exit -v --verbose print status messages -q --quiet report only file names -r --repeat show all occurrences of the same error --exclude=PATTERNS exclude files or directories which match these comma separated patterns [default: .svn,CVS,.bzr,.hg,.git] -f NAME --file=NAME when parsing directories, only check filenames matching these comma separated patterns [default: *.py] [type: path] --select=ERRORS select errors and warnings [choices: E W6] --ignore=ERRORS skip errors and warnings [choices: E4 W] --show-source show source code for each error --statistics count errors and warnings --count print total number of errors and warnings to standard error and set exit code to 1 if total is not null --benchmark measure processing speed --testsuite=DIR run regression tests from dir [type: path] --doctest run doctest on myself """ from type_docopt import docopt from pathlib import Path if __name__ == "__main__": arguments = docopt(__doc__, version="1.0.0rc2", types={"path": Path}) print(arguments)
def test_long_options_error_handling(): # with raises(DocoptLanguageError): # docopt('Usage: prog --non-existent', '--non-existent') # with raises(DocoptLanguageError): # docopt('Usage: prog --non-existent') with raises(DocoptExit): docopt("Usage: prog", "--non-existent") with raises(DocoptExit): docopt( "Usage: prog [--version --verbose]\n" "Options: --version\n --verbose", "--ver", ) with raises(DocoptLanguageError): docopt("Usage: prog --long\nOptions: --long ARG") with raises(DocoptExit): docopt("Usage: prog --long ARG\nOptions: --long ARG", "--long") with raises(DocoptLanguageError): docopt("Usage: prog --long=ARG\nOptions: --long") with raises(DocoptExit): docopt("Usage: prog --long\nOptions: --long", "--long=ARG")
def test_matching_paren(): with raises(DocoptLanguageError): docopt("Usage: prog [a [b]") with raises(DocoptLanguageError): docopt("Usage: prog [a [b] ] c )")
def test_language_errors(): with raises(DocoptLanguageError): docopt("no usage with colon here") with raises(DocoptLanguageError): docopt("usage: here \n\n and again usage: here")
for key, value in config.items("default-arguments")) def merge(dict_1, dict_2): """Merge two dictionaries. Values that evaluate to true take priority over falsy values. `dict_1` takes priority over `dict_2`. """ return dict((str(key), dict_1.get(key) or dict_2.get(key)) for key in set(dict_2) | set(dict_1)) if __name__ == "__main__": json_config = load_json_config() ini_config = load_ini_config() arguments = docopt(__doc__, version="0.1.1rc") # Arguments take priority over INI, INI takes priority over JSON: result = merge(arguments, merge(ini_config, json_config)) from pprint import pprint print("\nJSON config:") pprint(json_config) print("\nINI config:") pprint(ini_config) print("\nResult:") pprint(result)
def test_issue_59(): assert docopt("usage: prog --long=<a>", "--long=") == {"--long": ""} assert docopt("usage: prog -l <a>\n" "options: -l <a>", ["-l", ""]) == { "-l": "" }
predict test Options: -h --help Show this. See 'python main.py <command> --help' for more information on a specific command. """ from pathlib import Path from type_docopt import docopt from utils import IntList if __name__ == "__main__": args = docopt(__doc__, options_first=True) argv = [args["<command>"]] + args["<args>"] if args["<command>"] == "build-vocabulary": from build_vocabulary import __doc__, build_vocabulary build_vocabulary(docopt(__doc__, argv=argv, types={"path": Path})) elif args["<command>"] == "train": from train import __doc__, train train( docopt(__doc__, argv=argv, types={ "path": Path,
"""Naval Fate. Usage: naval_fate.py ship new <name>... naval_fate.py ship <name> move <x> <y> [--speed=<kn>] naval_fate.py ship shoot <x> <y> naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting] naval_fate.py -h | --help naval_fate.py --version Options: -h --help Show this screen. --version Show version. --speed=<kn> Speed in knots [default: 10] [type: int]. --moored Moored (anchored) mine. --drifting Drifting mine. """ from type_docopt import docopt if __name__ == "__main__": arguments = docopt(__doc__, version="Naval Fate 2.0") print(arguments)