def docopt(doc, argv=None, help=True, version=None, options_first=False): # @ReservedAssignment help """Re-implementation of docopt.docopt() function to parse ANYTHING at the end (for proxying django options).""" if argv is None: argv = sys.argv[1:] DocoptExit.usage = printable_usage(doc) options = parse_defaults(doc) pattern = parse_pattern(formal_usage(DocoptExit.usage), options) argv = parse_argv(TokenStream(argv, DocoptExit), list(options), options_first) pattern_options = set(pattern.flat(Option)) for ao in pattern.flat(AnyOptions): doc_options = parse_defaults(doc) ao.children = list(set(doc_options) - pattern_options) extras(help, version, argv, doc) __matched, __left, collected = pattern.fix().match(argv) # if matched and left == []: # better error message if left? if collected: # better error message if left? result = Dict((a.name, a.value) for a in (pattern.flat() + collected)) collected_django_options = len(result.get("DJANGO_OPTIONS", [])) result["DJANGO_OPTIONS"] = ( result.get("DJANGO_OPTIONS", []) + sys.argv[len(collected) + (collected_django_options or 1) :] ) # If any of the collected arguments are also in the DJANGO_OPTIONS, # then exit because we don't want users to have put options for kalite # at the end of the command if any(map(lambda x: x.name in map(lambda x: x.split("=")[0], result["DJANGO_OPTIONS"]), collected)): sys.stderr.write( "Cannot mix django manage command options with kalite options. " "Always put django management options last.\n\n" ) raise DocoptExit() return result raise DocoptExit()
def parse_cli(): """Parse the CLI arguments and options.""" import phabfive from docopt import extras, Option, DocoptExit try: cli_args = docopt(base_args, options_first=True, version=phabfive.__version__, help=True) except DocoptExit: extras(True, phabfive.__version__, [Option("-h", "--help", 0, True)], base_args) argv = [cli_args["<command>"]] + cli_args["<args>"] patterns = re.compile("^(?:" + "|".join(MONOGRAMS.values()) + ")") # First check for monogram shortcuts, i.e. invocation with `phabfive K123` # instead of the full `phabfive passphrase K123` if patterns.search(cli_args["<command>"]): monogram = cli_args["<command>"] app = {MONOGRAMS[k][0]: k for k in MONOGRAMS.keys()}[monogram[0]] # Patch the arguments to fool docopt into thinking we are the app if app == "passphrase": argv = [app] + argv elif app == "diffusion": argv = [app, "branch", "list"] + argv elif app == "paste": argv = [app, "show"] + argv elif app == "user": argv = [app, "whoami"] + argv cli_args["<args>"] = [monogram] cli_args["<command>"] = app sub_args = docopt(eval("sub_{app}_args".format(app=app)), argv=argv) elif cli_args["<command>"] == "passphrase": sub_args = docopt(sub_passphrase_args, argv=argv) elif cli_args["<command>"] == "diffusion": sub_args = docopt(sub_diffusion_args, argv=argv) elif cli_args["<command>"] == "paste": sub_args = docopt(sub_paste_args, argv=argv) elif cli_args["<command>"] == "user": sub_args = docopt(sub_user_args, argv=argv) else: extras(True, phabfive.__version__, [Option("-h", "--help", 0, True)], base_args) sys.exit(1) sub_args["<sub_command>"] = cli_args["<args>"][0] return (cli_args, sub_args)
def docopt(doc, argv=None, help=True, version=None, options_first=False): # @ReservedAssignment help """Re-implementation of docopt.docopt() function to parse ANYTHING at the end (for proxying django options).""" if argv is None: argv = sys.argv[1:] DocoptExit.usage = printable_usage(doc) options = parse_defaults(doc) pattern = parse_pattern(formal_usage(DocoptExit.usage), options) argv = parse_argv(TokenStream(argv, DocoptExit), list(options), options_first) pattern_options = set(pattern.flat(Option)) for ao in pattern.flat(AnyOptions): doc_options = parse_defaults(doc) ao.children = list(set(doc_options) - pattern_options) extras(help, version, argv, doc) __matched, __left, collected = pattern.fix().match(argv) # if matched and left == []: # better error message if left? if collected: # better error message if left? result = Dict((a.name, a.value) for a in (pattern.flat() + collected)) collected_django_options = len(result.get('DJANGO_OPTIONS', [])) result['DJANGO_OPTIONS'] = (result.get('DJANGO_OPTIONS', []) + sys.argv[len(collected) + (collected_django_options or 1):]) # If any of the collected arguments are also in the DJANGO_OPTIONS, # then exit because we don't want users to have put options for kalite # at the end of the command if any( map( lambda x: x.name in map(lambda x: x.split("=")[0], result[ 'DJANGO_OPTIONS']), collected)): sys.stderr.write( "Cannot mix django manage command options with kalite options. " "Always put django management options last.\n\n") raise DocoptExit() return result raise DocoptExit()
def _print_usage(text): import holmscan extras(True, holmscan.__version__, [Option("-h", "--help", 0, True)], text)