Ejemplo n.º 1
0
def option_parser(defaults=None, usage=USAGE):
    """
    Make up an option and arguments parser.

    :param defaults: Default option values
    :param usage: Usage text
    """
    defaults = dict(loglevel=1, list=False, output=None, itype=None,
                    otype=None, atype=None, merge=A.MS_DICTS,
                    ignore_missing=False, template=False, env=False,
                    schema=None, validate=False, gen_schema=False)

    ctypes = A.list_types()
    ctypes_s = ", ".join(ctypes)
    type_help = "Select type of %s config files from " + \
        ctypes_s + " [Automatically detected by file ext]"

    mts = A.MERGE_STRATEGIES
    mts_s = ", ".join(mts)
    mt_help = "Select strategy to merge multiple configs from " + \
        mts_s + " [%(merge)s]" % defaults

    af_help = """Explicitly select type of argument to provide configs from %s.

If this option is not set, original parser is used: 'K:V' will become {K: V},
'K:V_0,V_1,..' will become {K: [V_0, V_1, ...]}, and 'K_0:V_0;K_1:V_1' will
become {K_0: V_0, K_1: V_1} (where the tyep of K is str, type of V is one of
Int, str, etc.""" % ctypes_s

    get_help = ("Specify key path to get part of config, for example, "
                "'--get a.b.c' to config {'a': {'b': {'c': 0, 'd': 1}}} "
                "gives 0 and '--get a.b' to the same config gives "
                "{'c': 0, 'd': 1}.")
    set_help = ("Specify key path to set (update) part of config, for "
                "example, '--set a.b.c=1' to a config {'a': {'b': {'c': 0, "
                "'d': 1}}} gives {'a': {'b': {'c': 1, 'd': 1}}}.")

    parser = optparse.OptionParser(usage, version="%%prog %s" %
                                   anyconfig.globals.VERSION)
    parser.set_defaults(**defaults)

    lpog = optparse.OptionGroup(parser, "List specific options")
    lpog.add_option("-L", "--list", help="List supported config types",
                    action="store_true")
    parser.add_option_group(lpog)

    spog = optparse.OptionGroup(parser, "Schema specific options")
    spog.add_option("", "--validate", action="store_true",
                    help="Only validate input files and do not output. "
                         "You must specify schema file with -S/--schema "
                         "option.")
    spog.add_option("", "--gen-schema", action="store_true",
                    help="Generate JSON schema for givne config file[s] "
                         "and output it instead of (merged) configuration.")
    parser.add_option_group(spog)

    gspog = optparse.OptionGroup(parser, "Get/set options")
    gspog.add_option("", "--get", help=get_help)
    gspog.add_option("", "--set", help=set_help)
    parser.add_option_group(gspog)

    parser.add_option("-o", "--output", help="Output file path")
    parser.add_option("-I", "--itype", choices=ctypes,
                      help=(type_help % "Input"))
    parser.add_option("-O", "--otype", choices=ctypes,
                      help=(type_help % "Output"))
    parser.add_option("-M", "--merge", choices=mts, help=mt_help)
    parser.add_option("-A", "--args", help="Argument configs to override")
    parser.add_option("", "--atype", choices=ctypes, help=af_help)

    parser.add_option("-x", "--ignore-missing", action="store_true",
                      help="Ignore missing input files")
    parser.add_option("-T", "--template", action="store_true",
                      help="Enable template config support")
    parser.add_option("-E", "--env", action="store_true",
                      help="Load configuration defaults from "
                           "environment values")
    parser.add_option("-S", "--schema", help="Specify Schema file[s] path")
    parser.add_option("-s", "--silent", action="store_const", dest="loglevel",
                      const=0, help="Silent or quiet mode")
    parser.add_option("-q", "--quiet", action="store_const", dest="loglevel",
                      const=0, help="Same as --silent option")
    parser.add_option("-v", "--verbose", action="store_const", dest="loglevel",
                      const=2, help="Verbose mode")

    return parser
Ejemplo n.º 2
0
def parse_args(argv=None, defaults=None):
    """
    Make up an option and arguments parser.

    :param defaults: Default option values
    """
    if defaults is None:
        defaults = DEFAULTS

    ctypes = API.list_types()
    ctypes_s = ", ".join(ctypes)
    type_help = "Select type of %s config files from " + \
        ctypes_s + " [Automatically detected by file ext]"

    mts = API.MERGE_STRATEGIES
    mts_s = ", ".join(mts)
    mt_help = "Select strategy to merge multiple configs from " + \
        mts_s + " [%(merge)s]" % defaults

    parser = optparse.OptionParser(USAGE, version="%%prog %s" %
                                   anyconfig.globals.VERSION)
    parser.set_defaults(**defaults)

    lpog = optparse.OptionGroup(parser, "List specific options")
    lpog.add_option("-L", "--list", help="List supported config types",
                    action="store_true")
    parser.add_option_group(lpog)

    spog = optparse.OptionGroup(parser, "Schema specific options")
    spog.add_option("", "--validate", action="store_true",
                    help="Only validate input files and do not output. "
                         "You must specify schema file with -S/--schema "
                         "option.")
    spog.add_option("", "--gen-schema", action="store_true",
                    help="Generate JSON schema for givne config file[s] "
                         "and output it instead of (merged) configuration.")
    parser.add_option_group(spog)

    gspog = optparse.OptionGroup(parser, "Get/set options")
    gspog.add_option("", "--get", help=_GET_HELP)
    gspog.add_option("", "--set", help=_SET_HELP)
    parser.add_option_group(gspog)

    parser.add_option("-o", "--output", help="Output file path")
    parser.add_option("-I", "--itype", choices=ctypes,
                      help=(type_help % "Input"))
    parser.add_option("-O", "--otype", choices=ctypes,
                      help=(type_help % "Output"))
    parser.add_option("-M", "--merge", choices=mts, help=mt_help)
    parser.add_option("-A", "--args", help="Argument configs to override")
    parser.add_option("", "--atype", choices=ctypes,
                      help=_ATYPE_HELP_FMT % ctypes_s)

    parser.add_option("-x", "--ignore-missing", action="store_true",
                      help="Ignore missing input files")
    parser.add_option("-T", "--template", action="store_true",
                      help="Enable template config support")
    parser.add_option("-E", "--env", action="store_true",
                      help="Load configuration defaults from "
                           "environment values")
    parser.add_option("-S", "--schema", help="Specify Schema file[s] path")
    parser.add_option("-s", "--silent", action="store_const", dest="loglevel",
                      const=0, help="Silent or quiet mode")
    parser.add_option("-q", "--quiet", action="store_const", dest="loglevel",
                      const=0, help="Same as --silent option")
    parser.add_option("-v", "--verbose", action="store_const", dest="loglevel",
                      const=2, help="Verbose mode")

    if argv is None:
        argv = sys.argv

    (options, args) = parser.parse_args(argv[1:])
    return (parser, options, args)
Ejemplo n.º 3
0
def parse_args(argv=None, defaults=None):
    """
    Make up an option and arguments parser.

    :param defaults: Default option values
    """
    if defaults is None:
        defaults = DEFAULTS

    ctypes = API.list_types()
    ctypes_s = ", ".join(ctypes)
    type_help = "Select type of %s config files from " + \
        ctypes_s + " [Automatically detected by file ext]"

    mts = API.MERGE_STRATEGIES
    mts_s = ", ".join(mts)
    mt_help = "Select strategy to merge multiple configs from " + \
        mts_s + " [%(merge)s]" % defaults

    parser = optparse.OptionParser(USAGE, version="%%prog %s" %
                                   anyconfig.globals.VERSION)
    parser.set_defaults(**defaults)

    lpog = optparse.OptionGroup(parser, "List specific options")
    lpog.add_option("-L", "--list", help="List supported config types",
                    action="store_true")
    parser.add_option_group(lpog)

    spog = optparse.OptionGroup(parser, "Schema specific options")
    spog.add_option("", "--validate", action="store_true",
                    help="Only validate input files and do not output. "
                         "You must specify schema file with -S/--schema "
                         "option.")
    spog.add_option("", "--gen-schema", action="store_true",
                    help="Generate JSON schema for givne config file[s] "
                         "and output it instead of (merged) configuration.")
    parser.add_option_group(spog)

    gspog = optparse.OptionGroup(parser, "Query/Get/set options")
    gspog.add_option("-Q", "--query", help=_QUERY_HELP)
    gspog.add_option("", "--get", help=_GET_HELP)
    gspog.add_option("", "--set", help=_SET_HELP)
    parser.add_option_group(gspog)

    parser.add_option("-o", "--output", help="Output file path")
    parser.add_option("-I", "--itype", choices=ctypes,
                      help=(type_help % "Input"))
    parser.add_option("-O", "--otype", choices=ctypes,
                      help=(type_help % "Output"))
    parser.add_option("-M", "--merge", choices=mts, help=mt_help)
    parser.add_option("-A", "--args", help="Argument configs to override")
    parser.add_option("", "--atype", choices=ctypes,
                      help=_ATYPE_HELP_FMT % ctypes_s)

    parser.add_option("-x", "--ignore-missing", action="store_true",
                      help="Ignore missing input files")
    parser.add_option("-T", "--template", action="store_true",
                      help="Enable template config support")
    parser.add_option("-E", "--env", action="store_true",
                      help="Load configuration defaults from "
                           "environment values")
    parser.add_option("-S", "--schema", help="Specify Schema file[s] path")
    parser.add_option("-s", "--silent", action="store_const", dest="loglevel",
                      const=0, help="Silent or quiet mode")
    parser.add_option("-q", "--quiet", action="store_const", dest="loglevel",
                      const=0, help="Same as --silent option")
    parser.add_option("-v", "--verbose", action="store_const", dest="loglevel",
                      const=2, help="Verbose mode")

    if argv is None:
        argv = sys.argv

    (options, args) = parser.parse_args(argv[1:])
    return (parser, options, args)