def main(): import argparse formats = loading.get_formats() parser = argparse.ArgumentParser() parser.print_usage = parser.print_help # hack parser.add_argument( "--log", choices=list(logging._nameToLevel.keys()), default="INFO", dest="log_level", ) parser.add_argument("-q", "--quiet", action="store_true") parser.add_argument("--debug", action="store_true") subparsers = parser.add_subparsers(dest="subcommand") subparsers.required = True # cut fn = cut sparser = subparsers.add_parser(fn.__name__, description=fn.__doc__) sparser.set_defaults(subcommand=fn) sparser.add_argument("--src", default=None) sparser.add_argument("--dst", default=None) sparser.add_argument("--ref", dest="refs", action="append") # deref fn = deref sparser = subparsers.add_parser(fn.__name__, description=fn.__doc__) sparser.set_defaults(subcommand=fn) sparser.add_argument("--src", default=None) sparser.add_argument("--dst", default=None) sparser.add_argument("--ref", dest="refs", action="append") sparser.add_argument("--unwrap", default=None) sparser.add_argument("--wrap", default=None) sparser.add_argument("-f", "--format", default=None, choices=formats) sparser.add_argument("-i", "--input-format", default=None, choices=formats) sparser.add_argument("-o", "--output-format", default=None, choices=formats) # select fn = select sparser = subparsers.add_parser(fn.__name__, description=fn.__doc__) sparser.set_defaults(subcommand=fn) sparser.add_argument("--src", default=None) sparser.add_argument("--dst", default=None) sparser.add_argument("--ref", dest="refs", action="append") sparser.add_argument("--unwrap", default=None) sparser.add_argument("--wrap", default=None) sparser.add_argument("-f", "--format", default=None, choices=formats) sparser.add_argument("-i", "--input-format", default=None, choices=formats) sparser.add_argument("-o", "--output-format", default=None, choices=formats) # bundle fn = bundle sparser = subparsers.add_parser(fn.__name__, description=fn.__doc__) sparser.set_defaults(subcommand=fn) sparser.add_argument("--src", default=None) sparser.add_argument("--dst", default=None) sparser.add_argument("--ref", default=None) sparser.add_argument("--flavor", choices=["openapiv3", "openapiv2"], default="openapiv3") sparser.add_argument("-f", "--format", default=None, choices=formats) sparser.add_argument("-i", "--input-format", default=None, choices=formats) sparser.add_argument("-o", "--output-format", default=None, choices=formats) sparser.add_argument("--extra", default=None, nargs="+", dest="extras") # separate fn = separate sparser = subparsers.add_parser(fn.__name__, description=fn.__doc__) sparser.set_defaults(subcommand=fn) sparser.add_argument("--src", default=None) sparser.add_argument("--dst", default=None) sparser.add_argument("-f", "--format", default=None, choices=formats) sparser.add_argument("-i", "--input-format", default=None, choices=formats) sparser.add_argument("-o", "--output-format", default=None, choices=formats) # examples fn = examples sparser = subparsers.add_parser(fn.__name__, description=fn.__doc__) sparser.set_defaults(subcommand=fn) sparser.add_argument("src", nargs="?", default=None) sparser.add_argument("--dst", default=None) sparser.add_argument("--ref", dest="ref", default=None) sparser.add_argument("--limit", dest="limit", default=5, type=int) sparser.add_argument("--expand", dest="use_expand", action="store_true") sparser.add_argument("-f", "--format", default=None, choices=formats) sparser.add_argument("-i", "--input-format", default=None, choices=formats) sparser.add_argument("-o", "--output-format", default=None, choices=formats) args = parser.parse_args() with contextlib.ExitStack() as s: params = vars(args) if params.pop("quiet"): args.log_level = logging._levelToName[logging.WARNING] s.enter_context(warnings.catch_warnings()) warnings.simplefilter("ignore") logging.basicConfig(level=getattr(logging, params.pop("log_level"))) with traceback_shortly(params.pop("debug")): return params.pop("subcommand")(**params)
def main(): import argparse formats = loading.get_formats() parser = argparse.ArgumentParser( formatter_class=type( "_HelpFormatter", (argparse.ArgumentDefaultsHelpFormatter, argparse.RawTextHelpFormatter), {}, ) ) parser.print_usage = parser.print_help # hack parser.add_argument( "--log", choices=list(logging._nameToLevel.keys()), default="INFO", dest="log_level", help="-", ) parser.add_argument("-q", "--quiet", action="store_true", help="-") parser.add_argument("--debug", action="store_true", help="-") # modification parser.add_argument( "--compact", action="store_true", dest="modification_compact", help="-" ) parser.add_argument( "--flatten", action="store_true", dest="modification_flatten", help="-" ) parser.add_argument( "--unescape", default=None, dest="modification_unescape", choices=["unicode", "url"], help="-", ) subparsers = parser.add_subparsers(dest="subcommand", title="subcommands") subparsers.required = True # cat fn = cat sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) apply_loading_format_extra_arguments_parser(sparser) sparser.set_defaults(subcommand=fn) sparser.add_argument("files", nargs="*", default=[sys.stdin], help="-") sparser.add_argument("--slurp", action="store_true", help="-") sparser.add_argument("--size", type=int, default=None, help="-") sparser.add_argument("--dst", default=None, help="-") sparser.add_argument("-f", "--format", default=None, choices=formats, help="-") sparser.add_argument( "-i", "--input-format", default=None, choices=formats, help="-" ) sparser.add_argument( "-o", "--output-format", default=None, choices=formats, help="-" ) sparser.add_argument( "--encoding", help="input encoding. (e.g. utf-8, cp932, ...)", default=None, ) sparser.add_argument( "--errors", default=None, choices=[ "strict", "ignore", "replace", "surrogateescape", "xmlcharrefreplace", "backslashreplace", "namereplace", ], help="see pydoc codecs.Codec", ) sparser.add_argument("-S", "--sort-keys", action="store_true", help="-") sparser.add_argument( "--merge-method", choices=["addtoset", "append", "merge", "replace"], default="addtoset", help="-", ) # transform fn = transform sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) def print_help(*, file=None, self=sparser): if file is None: file = sys.stdout # overwrite help for ac in self._actions: if ac.dest == "functions": import dictknife.transform as m callables = [ k for k, v in m.__dict__.items() if not k.startswith("_") and callable(v) ] ac.help = "(e.g. {}, ... or <module>:<fn name> (e.g. dictknife.transform:flatten))".format( ", ".join(sorted(callables)) ) self._print_message(self.format_help(), file) sparser.print_help = print_help sparser.set_defaults(subcommand=fn) sparser.add_argument("src", nargs="?", help="-") sparser.add_argument("--dst", default=None, help="-") sparser.add_argument("--code", default=None, help="-") sparser.add_argument( "--fn", "--function", default=[], action="append", dest="functions", help="-" ) sparser.add_argument( "-i", "--input-format", default=None, choices=formats, help="-" ) sparser.add_argument( "-o", "--output-format", default=None, choices=formats, help="-" ) sparser.add_argument("-f", "--format", default=None, choices=formats, help="-") sparser.add_argument("-S", "--sort-keys", action="store_true", help="-") # diff fn = diff sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) sparser.set_defaults(subcommand=fn) sparser.add_argument("--normalize", action="store_true", help="-") sparser.add_argument("--verbose", action="store_true", help="-") sparser.add_argument("left", help="-") sparser.add_argument("right", help="-") sparser.add_argument("--n", default=3, type=int, help="-") sparser.add_argument("--skip-empty", action="store_true", help="-") sparser.add_argument( "-i", "--input-format", default=None, choices=formats, help="-" ) sparser.add_argument( "-o", "--output-format", choices=["diff", "dict", "md", "tsv", "jsonpatch"], default="diff", help="-", ) sparser.add_argument("-S", "--sort-keys", action="store_true", help="-") # shape fn = shape sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) sparser.set_defaults(subcommand=fn) sparser.add_argument("files", nargs="*", default=[sys.stdin], help="-") sparser.add_argument("--squash", action="store_true", help="-") sparser.add_argument("--skiplist", action="store_true", help="-") sparser.add_argument("--full", action="store_true", help="-") sparser.add_argument("--with-type", action="store_true", help="-") sparser.add_argument("--with-example", action="store_true", help="-") sparser.add_argument("--separator", default="/", help="-") sparser.add_argument( "-i", "--input-format", default=None, choices=formats, help="-" ) sparser.add_argument( "-o", "--output-format", default=None, choices=formats, help="-" ) # shrink fn = shrink sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) sparser.set_defaults(subcommand=fn) sparser.add_argument("files", nargs="*", default=[sys.stdin], help="-") sparser.add_argument("--max-length-of-string", type=int, default=100, help="-") sparser.add_argument("--max-length-of-list", type=int, default=3, help="-") sparser.add_argument("--cont-suffix", default="...", help="-") sparser.add_argument("--with-tail", action="store_true", help="-") sparser.add_argument( "-i", "--input-format", default=None, choices=formats, help="-" ) sparser.add_argument( "-o", "--output-format", default=None, choices=formats, help="-" ) # mkdict fn = mkdict sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) apply_rest_arguments_as_extra_arguments_parser(sparser) sparser.set_defaults(subcommand=fn) sparser.add_argument("--squash", action="store_true", help="-") sparser.add_argument( "-o", "--output-format", default="json", choices=formats, help="-" ) sparser.add_argument("--separator", default="/", help="-") sparser.add_argument("--delimiter", default=";", help="-") sparser.add_argument("-S", "--sort-keys", action="store_true", help="-") args = parser.parse_args() params = vars(args) with contextlib.ExitStack() as s: if params.pop("quiet"): args.log_level = logging._levelToName[logging.WARNING] s.enter_context(warnings.catch_warnings()) warnings.simplefilter("ignore") logging.basicConfig(level=getattr(logging, params.pop("log_level"))) with traceback_shortly(params.pop("debug")): # apply modification for k in list(params.keys()): if k.startswith("modification_"): v = params.pop(k) if v: from importlib import import_module if isinstance(v, str): module_path = "dictknife.loading.{}_{}".format( k.replace("_", "."), v ) else: module_path = "dictknife.loading.{}".format( k.replace("_", ".") ) logger.info("apply %s.setup()", module_path) m = import_module(module_path) if not hasattr(m, "setup"): raise RuntimeError( "{}:setup() is not found".format(module_path) ) m.setup(loading.dispatcher) return params.pop("subcommand")(**params)
def main(): import argparse formats = loading.get_formats() parser = argparse.ArgumentParser( formatter_class=type( "_HelpFormatter", (argparse.ArgumentDefaultsHelpFormatter, argparse.RawTextHelpFormatter), {}, ) ) parser.print_usage = parser.print_help # hack parser.add_argument( "--log", choices=list(logging._nameToLevel.keys()), default="INFO", dest="log_level", help="-", ) parser.add_argument("-q", "--quiet", action="store_true", help="-") parser.add_argument("--debug", action="store_true", help="-") subparsers = parser.add_subparsers(dest="subcommand", title="subcommands") subparsers.required = True # merge fn = merge sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) sparser.set_defaults(subcommand=fn) sparser.add_argument("files", nargs="*", default=None, help="-") sparser.add_argument("--dst", default=None, help="-") sparser.add_argument("--strict", action="store_true", help="-") sparser.add_argument("--style", default="ref", choices=["ref", "whole"], help="-") sparser.add_argument("--wrap", default=None, help="-") sparser.add_argument("--wrap-section", default="definitions", help="-") # tojsonschema fn = tojsonschema sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) sparser.set_defaults(subcommand=fn) sparser.add_argument("--src", default=None, help="-") sparser.add_argument("--dst", default=None, help="-") sparser.add_argument("--name", default="top", help="-") # json2swagger fn = json2swagger sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) sparser.set_defaults(subcommand=fn) sparser.add_argument("files", nargs="*", default=None, help="-") sparser.add_argument("--dst", default=None, help="-") sparser.add_argument( "-o", "--output-format", default=None, choices=formats, help="-" ) sparser.add_argument("--name", default="top", help="-") sparser.add_argument("--detector", default="Detector", help="-") sparser.add_argument("--emitter", default="Emitter", help="-") sparser.add_argument("--annotate", default=None, help="-") sparser.add_argument( "--emit", default="schema", choices=["schema", "info"], help="-" ) sparser.add_argument("--with-minimap", action="store_true", help="-") sparser.add_argument("--without-example", action="store_true", help="-") # flatten fn = flatten sparser = subparsers.add_parser( fn.__name__, help=fn.__doc__, formatter_class=parser.formatter_class ) sparser.set_defaults(subcommand=fn) sparser.add_argument("src", nargs="?", default=None, help="-") sparser.add_argument("--dst", default=None, help="-") sparser.add_argument( "-i", "--input-format", default=None, choices=formats, help="-" ) sparser.add_argument( "-o", "--output-format", default=None, choices=formats, help="-" ) sparser.add_argument("-f", "--format", default=None, choices=formats, help="-") args = parser.parse_args() with contextlib.ExitStack() as s: params = vars(args) if params.pop("quiet"): args.log_level = logging._levelToName[logging.WARNING] s.enter_context(warnings.catch_warnings()) warnings.simplefilter("ignore") logging.basicConfig(level=getattr(logging, params.pop("log_level"))) with traceback_shortly(params.pop("debug")): return params.pop("subcommand")(**params)