Python argparse.ArgumentParser() Examples

The following are code examples for showing how to use argparse.ArgumentParser(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: download_glue.py    Apache License 2.0 6 votes vote down vote up
def main(arguments):
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_dir', help='directory to save data to', type=str, default='glue_data')
    parser.add_argument('--tasks', help='tasks to download data for as a comma separated string',
                        type=str, default='all')
    parser.add_argument('--path_to_mrpc',
                        help='path to directory containing extracted MRPC data, msr_paraphrase_train.txt and msr_paraphrase_text.txt',
                        type=str, default='')
    args = parser.parse_args(arguments)

    if not os.path.isdir(args.data_dir):
        os.mkdir(args.data_dir)
    tasks = get_tasks(args.tasks)

    for task in tasks:
        if task == 'MRPC':
            format_mrpc(args.data_dir, args.path_to_mrpc)
        elif task == 'diagnostic':
            download_diagnostic(args.data_dir)
        else:
            download_and_extract(task, args.data_dir) 
Example 2
Project: mutatest   Author: EvanKepner   File: cli.py    MIT License 6 votes vote down vote up
def cli_args(
    args: Sequence[str], ini_config_file: Path = Path("mutatest.ini")
) -> argparse.Namespace:
    """Command line arguments as parsed args.

    If a INI configuration file is set it is used to set additional default arguments, but
    the CLI arguments override any INI file settings.

    Args:
        args: the argument sequence from the command line
        ini_config_file: the ini config file for the default settings

    Returns:
        Parsed args from ArgumentParser
    """
    parser = cli_parser()

    if ini_config_file.exists():
        ini_config = read_ini_config(ini_config_file)
        ini_cli_args = parse_ini_config_with_cli(parser, ini_config, args)
        return parser.parse_args(ini_cli_args)

    return parser.parse_args(args) 
Example 3
Project: incubator-spot   Author: apache   File: worker.py    Apache License 2.0 6 votes vote down vote up
def main():

    # input parameters
    parser = argparse.ArgumentParser(description="Worker Ingest Framework")
    parser.add_argument('-t', '--type', dest='type', required=True,
                        help='Type of data that will be ingested (Pipeline Configuration)',
                        metavar='')
    parser.add_argument('-i', '--id', dest='id', required=True,
                        help='Worker Id, this is needed to sync Kafka and Ingest framework (Partition Number)',
                        metavar='')
    parser.add_argument('-top', '--topic', dest='topic', required=True,
                        help='Topic to read from.', metavar="")
    parser.add_argument('-p', '--processingParallelism', dest='processes',
                        required=False, help='Processing Parallelism', metavar="")
    args = parser.parse_args()

    # start worker based on the type.
    start_worker(args.type, args.topic, args.id, args.processes) 
Example 4
Project: incubator-spot   Author: apache   File: bluecoat.py    Apache License 2.0 6 votes vote down vote up
def main():
    """
    Handle commandline arguments and
    start the collector.
    """
    # input Parameters
    parser = argparse.ArgumentParser(description="Bluecoat Parser")
    parser.add_argument('-zk', '--zookeeper', dest='zk', required=True,
                        help='Zookeeper IP and port (i.e. 10.0.0.1:2181)', metavar='')
    parser.add_argument('-t', '--topic', dest='topic', required=True,
                        help='Topic to listen for Spark Streaming', metavar='')
    parser.add_argument('-db', '--database', dest='db', required=True,
                        help='Hive database whete the data will be ingested', metavar='')
    parser.add_argument('-dt', '--db-table', dest='db_table', required=True,
                        help='Hive table whete the data will be ingested', metavar='')
    parser.add_argument('-w', '--num_of_workers', dest='num_of_workers', required=True,
                        help='Num of workers for Parallelism in Data Processing', metavar='')
    parser.add_argument('-bs', '--batch-size', dest='batch_size', required=True,
                        help='Batch Size (Milliseconds)', metavar='')
    args = parser.parse_args()

    # start collector based on data source type.
    bluecoat_parse(args.zk, args.topic, args.db, args.db_table, args.num_of_workers, args.batch_size) 
Example 5
Project: incubator-spot   Author: apache   File: master_collector.py    Apache License 2.0 6 votes vote down vote up
def main():

    # input Parameters
    parser = argparse.ArgumentParser(description="Master Collector Ingest Daemon")
    parser.add_argument('-t', '--type', dest='type', required=True,
                        help='Type of data that will be ingested (Pipeline Configuration)',
                        metavar='')
    parser.add_argument('-w', '--workers', dest='workers_num',
                        required=True, help='Number of workers for the ingest process',
                        metavar='')
    parser.add_argument('-id', '--ingestId', dest='ingest_id',
                        required=False, help='Ingest ID', metavar='')
    args = parser.parse_args()

    # start collector based on data source type.
    start_collector(args.type, args.workers_num, args.ingest_id) 
Example 6
Project: webnull   Author: macrael   File: webnull.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def arg_parser():
    parser = argparse.ArgumentParser(description='A tool for putting websites into a black hole.')
    commands = parser.add_subparsers(title='commands', metavar='COMMAND')
    commands.required = True
    commands.dest = "commands"

    deny = commands.add_parser('deny', description='Add a site to the black hole. It will become unreachable.', help='Add a site to the black hole. It will become unreachable.')
    deny.add_argument('sitename', help='The website to be blackholed. A URL will be stripped down correctly.')
    deny.set_defaults(func=deny_site)

    allow = commands.add_parser('allow', description='Allow access to a blackholed site for a spell.', help='Allow access to a blackholed site for a spell.')
    time_or_tomorrow = allow.add_mutually_exclusive_group()
    time_or_tomorrow.add_argument('-t', '--time', help='sets the duration to enable a site for. Default is five minutes.', default=5, type=int)
    time_or_tomorrow.add_argument('-m', '--morning', help='allow all sites until tomorrow morning at 6am', action='store_true')
    all_or_one = allow.add_mutually_exclusive_group(required=True)
    all_or_one.add_argument('-a', '--all', action='store_true', help='All blackholed hostnames will be granted access instead of a matching sitename.')
    all_or_one.add_argument('sitename', help='All blackholed hostnames that contain this string will be temporarlly granted access.', nargs='?')
    allow.set_defaults(func=allow_site)

    return parser 
Example 7
Project: godot-mono-builds   Author: godotengine   File: cmd_utils.py    MIT License 6 votes vote down vote up
def build_arg_parser(description, env_vars={}):
    from argparse import ArgumentParser, RawDescriptionHelpFormatter
    from textwrap import dedent

    base_env_vars = {
        'MONO_SOURCE_ROOT': 'Overrides default value for --mono-sources',
    }

    env_vars_text = '\n'.join(['    %s: %s' % (var, desc) for var, desc in env_vars.items()])
    base_env_vars_text = '\n'.join(['    %s: %s' % (var, desc) for var, desc in base_env_vars.items()])

    epilog=dedent('''\
environment variables:
%s
%s
''' % (env_vars_text, base_env_vars_text))

    return ArgumentParser(
        description=description,
        formatter_class=RawDescriptionHelpFormatter,
        epilog=epilog
    ) 
Example 8
Project: projection-methods   Author: akshayka   File: save_random_lp.py    GNU General Public License v3.0 6 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    # --- input/output --- #
    parser.add_argument(
        'output', metavar='O', type=str,
        help='output path specifying location in which to problem.')
    # --- problem parameters --- #
    parser.add_argument(
        'm', metavar='rows', type=int, help='number of rows in data matrix')
    parser.add_argument(
        'n', metavar='cols', type=int, help='number of variables')
    parser.add_argument(
        '-d', '--density', type=float, default=.01, help='density of data '
        'matrix A')
    args = parser.parse_args()

    path = check_path(args.output)
    lp = random_linear_program(m=args.m, n=args.n, density=args.density)
    save_problem(path, lp) 
Example 9
Project: pyblish-win   Author: pyblish   File: _uninstall.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _main(argv=None):
    parser = argparse.ArgumentParser(prog="python -m ensurepip._uninstall")
    parser.add_argument(
        "--version",
        action="version",
        version="pip {}".format(ensurepip.version()),
        help="Show the version of pip this will attempt to uninstall.",
    )
    parser.add_argument(
        "-v", "--verbose",
        action="count",
        default=0,
        dest="verbosity",
        help=("Give more output. Option is additive, and can be used up to 3 "
              "times."),
    )

    args = parser.parse_args(argv)

    ensurepip._uninstall_helper(verbosity=args.verbosity) 
Example 10
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test(self):

        def get_my_type(string):
            return 'my_type{%s}' % string

        parser = argparse.ArgumentParser()
        parser.register('type', 'my_type', get_my_type)
        parser.add_argument('-x', type='my_type')
        parser.add_argument('y', type='my_type')

        self.assertEqual(parser.parse_args('1'.split()),
                         NS(x=None, y='my_type{1}'))
        self.assertEqual(parser.parse_args('-x 1 42'.split()),
                         NS(x='my_type{1}', y='my_type{42}'))


# ============
# Action tests
# ============ 
Example 11
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_misc(self):
        parser = argparse.ArgumentParser()
        action = parser.add_argument('--foo', nargs='?', const=42,
                                     default=84, type=int, choices=[1, 2],
                                     help='FOO', metavar='BAR', dest='baz')
        self.assertEqual(action.nargs, '?')
        self.assertEqual(action.const, 42)
        self.assertEqual(action.default, 84)
        self.assertEqual(action.type, int)
        self.assertEqual(action.choices, [1, 2])
        self.assertEqual(action.help, 'FOO')
        self.assertEqual(action.metavar, 'BAR')
        self.assertEqual(action.dest, 'baz')


# ================================
# Argument conflict handling tests
# ================================ 
Example 12
Project: DnD4py   Author: bacook17   File: lookup_5e.py    MIT License 6 votes vote down vote up
def monster_lookup():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=(
            """Searches Roll20.net 5e monster compendium for the term queried."""))
    parser.add_argument('query', nargs='+', help='The words to search')

    args = parser.parse_args()
    text = ' '.join(args.query)

    try:
        item = Roll20Monster(text)
    except IOError as e:
        print(e)
        item = None
    if item is None or len(item) == 0:
        return False
    else:
        try:
            print(item)
        except UnicodeEncodeError:
            print(item.as_unicode)
        return True 
Example 13
Project: DnD4py   Author: bacook17   File: lookup_5e.py    MIT License 6 votes vote down vote up
def item_lookup():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=(
            """Searches Roll20.net 5e Item compendium for the term queried."""))
    parser.add_argument('query', nargs='+', help='The words to search')

    args = parser.parse_args()
    text = ' '.join(args.query)
    
    try:
        item = Roll20Item(text)
    except IOError as e:
        print(e)
        item = None
    if item is None or len(item) == 0:
        return False
    else:
        try:
            print(item)
        except UnicodeEncodeError:
            print(item.as_unicode)
        return True 
Example 14
Project: decli   Author: Woile   File: application.py    MIT License 6 votes vote down vote up
def cli(
    data: dict,
    parser_class: Callable = argparse.ArgumentParser,
    parents: Optional[list] = None,
):
    """Create a cli application.

    This is the entrypoint.
    """
    global config
    config = init_config()
    data = deepcopy(data)

    if data.get("prefix_chars"):
        config.update({"prefix_chars": data.get("prefix_chars")})

    parser = add_parser(data, parser_class, parents)
    return parser 
Example 15
Project: lora-sx1276   Author: raspberrypi-tw   File: LoRaArgumentParser.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def parse_args(self, lora):
        """ Parse the args, perform some sanity checks and configure the LoRa accordingly.
        :param lora: Reference to LoRa object
        :return: args
        """
        args = argparse.ArgumentParser.parse_args(self)
        args.bw = self.bw_lookup.get(args.bw, None)
        args.coding_rate = self.cr_lookup.get(args.coding_rate, None)
        # some sanity checks
        assert(args.bw is not None)
        assert(args.coding_rate is not None)
        assert(args.sf >=6 and args.sf <= 12)
        # set the LoRa object
        lora.set_freq(args.freq)
        lora.set_preamble(args.preamble)
        lora.set_spreading_factor(args.sf)
        lora.set_bw(args.bw)
        lora.set_coding_rate(args.coding_rate)
        lora.set_ocp_trim(args.ocp)
        return args 
Example 16
Project: Mastering-Python-Networking-Second-Edition   Author: PacktPublishing   File: eapi_2_acl.py    MIT License 6 votes vote down vote up
def main():
   parser = argparse.ArgumentParser(description="Edit Arista ACLs using your local editor")
   parser.add_argument("acl", metavar="ACL",
                       help="Name of the access list to modify")
   parser.add_argument("switches", metavar="SWITCH", nargs="+",
                       help="Hostname or IP of the switch to query")
   parser.add_argument("--username", help="Name of the user to connect as",
                       default="admin")
   parser.add_argument("--password", help="The user's password")
   parser.add_argument("--https", help="Use HTTPS instead of HTTP",
                       action="store_const", const="https", default="http")
   args = parser.parse_args()

   aclName = args.acl
   tmpfile = "/tmp/AclEditor-%s" % aclName
   apiEndpoints = getEndpoints(args.switches, args.https,
                               args.username, args.password)
   prepopulateAclFile(tmpfile, aclName, apiEndpoints)
   edits = getEdits(tmpfile)
   applyChanges(aclName, apiEndpoints, edits)
   print
   print "Done!" 
Example 17
Project: BASS   Author: Cisco-Talos   File: cmdline.py    GNU General Public License v2.0 6 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description = "Bass")
    parser.add_argument("-v", "--verbose", action = "count", default = 0, help = "Increase verbosity")
    parser.add_argument("samples", metavar = "sample", nargs = "+", help = "Sample path") 

    args = parser.parse_args()

    try:
        loglevel = {
            0: logging.ERROR,
            1: logging.WARN,
            2: logging.INFO
        }[args.verbose]
    except KeyError:
        loglevel = logging.DEBUG

    logging.basicConfig(level = loglevel)
    logging.getLogger().setLevel(loglevel)

    return args 
Example 18
Project: BASS   Author: Cisco-Talos   File: whitelist.py    GNU General Public License v2.0 6 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description = "Add samples to BASS whitelist")
    parser.add_argument("-v", "--verbose", action = "count", default = 0, help = "Increase verbosity")
    parser.add_argument("--url", type = str, default = "http://localhost:5000", help = "URL of BASS server")
    parser.add_argument("sample", help = "Whitelist sample")

    args = parser.parse_args()

    try:
        loglevel = {
            0: logging.ERROR,
            1: logging.WARN,
            2: logging.INFO}[args.verbose]
    except KeyError:
        loglevel = logging.DEBUG
    logging.basicConfig(level = loglevel)
    logging.getLogger().setLevel(loglevel)

    return args 
Example 19
Project: BASS   Author: Cisco-Talos   File: client.py    GNU General Public License v2.0 6 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description = "Find common ngrams in binary files")
    parser.add_argument("-v", "--verbose", action = "count", default = 0, help = "Increase verbosity")
    parser.add_argument("--output", type = str, default = None, help = "Output to file instead of stdout")
    parser.add_argument("--url", type = str, default = "http://localhost:5000", help = "URL of BASS server")
    parser.add_argument("samples", metavar = "sample", nargs = "+", help = "Cluster samples")

    args = parser.parse_args()

    try:
        loglevel = {
            0: logging.ERROR,
            1: logging.WARN,
            2: logging.INFO}[args.verbose]
    except KeyError:
        loglevel = logging.DEBUG
    logging.basicConfig(level = loglevel)
    logging.getLogger().setLevel(loglevel)

    return args 
Example 20
Project: BASS   Author: Cisco-Talos   File: export_binexport_pickle.py    GNU General Public License v2.0 6 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description = "IDA Pro script: Dump bindiff database file")
    subparsers = parser.add_subparsers(help = "subcommand")
    
    parser_pickle = subparsers.add_parser("pickle", help = "Dump pickled database")
    parser_pickle.add_argument("pickle_output", type = str, help = "Output pickle database file")
    parser_pickle.set_defaults(handler = handle_pickle)

    parser_bindiff = subparsers.add_parser("binexport", help = "Dump bindiff database")
    parser_bindiff.add_argument("bindiff_output", type = str, help = "Output BinExport database file")
    parser_bindiff.set_defaults(handler = handle_binexport)

    parser_bindiff_pickle = subparsers.add_parser("binexport_pickle", help = "Dump bindiff database and pickled database")
    parser_bindiff_pickle.add_argument("bindiff_output", type = str, help = "Output BinDiff database file")
    parser_bindiff_pickle.add_argument("pickle_output", type = str, help = "Output pickle database file")
    parser_bindiff_pickle.set_defaults(handler = handle_binexport_pickle)

    args = parser.parse_args(idc.ARGV[1:])

    return args 
Example 21
Project: ff-net-worth-calculator   Author: blocktrron   File: cli.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def run():
    parser = argparse.ArgumentParser(description='Calculate net-worth of a Freifunk community')
    parser.add_argument('--url', help='URL to evaluate')
    parser.add_argument('--output-json', help='Stats as JSON', action='store_true')
    parser.add_argument('--exclude-domain', help='Domains to exclude for calculation', action="append")
    args = parser.parse_args()

    if args.url:
        result = load(args.url, args.exclude_domain)
    else:
        parser.print_help()
        sys.exit(1)

    if not result:
        print("No nodes found", file=sys.stderr)
        sys.exit(2)

    model_information = load_devices_json(get_data_path('devices.json'))
    community_information = gather_information(model_information, result)

    print_information(args.url, community_information, not args.output_json) 
Example 22
Project: RelayBot2.0   Author: nukeop   File: relaybot.py    GNU General Public License v3.0 6 votes vote down vote up
def main():
    """RelayBot 2.0 main entry point.
    Creates a new instance of the bot and runs it.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("--logfile", help="rotating log filename")
    args = parser.parse_args()

    bot = Bot(args.logfile)

    logger.info("Starting Relay Bot 2.0")
    logger.info("Version: {}.{}".format(VERSION[0], VERSION[1]))

    bot.initialize()

    bot.run() 
Example 23
Project: Pyssql   Author: bicofino   File: pyssql.py    MIT License 6 votes vote down vote up
def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--username')
        parser.add_argument('--password')
        parser.add_argument('--address')
        parser.add_argument('--port')
        parser.add_argument('--database')

        subparsers = parser.add_subparsers()

        for name in dir(self):
            if not name.startswith("_"):
                p = subparsers.add_parser(name)
                method = getattr(self, name)
                argnames = inspect.getargspec(method).args[1:]
                for argname in argnames:
                    p.add_argument(argname)
                p.set_defaults(func=method, argnames=argnames)
        self.args = parser.parse_args() 
Example 24
Project: iglovikov_segmentation   Author: ternaus   File: inference.py    MIT License 5 votes vote down vote up
def get_args():
    parser = argparse.ArgumentParser()
    arg = parser.add_argument
    arg("-c", "--config_path", type=Path, required=True, help="Path to  config")
    arg("-i", "--input_path", type=Path, required=True, help="Path to the target image folder.")
    arg("-j", "--num_workers", type=int, help="Number of CPU threads.", default=12)
    arg("-o", "--output_path", type=Path, help="Path where to save resulting masks.", required=True)
    arg("-w", "--checkpoint", type=Path, help="Path to checkpoint.", required=True)
    arg("-b", "--batch_size", type=int, help="Batch_size.", default=1)
    arg("-t", "--tta", help="Test time augmentation.", default=None, choices=[None, "d4", "lr"])
    arg("-v", "--visualize", help="If add visualized predictions.", action="store_true")
    return parser.parse_args() 
Example 25
Project: iglovikov_segmentation   Author: ternaus   File: train.py    MIT License 5 votes vote down vote up
def get_args():
    parser = argparse.ArgumentParser()
    arg = parser.add_argument
    arg("-c", "--config_path", type=Path, help="Path to the config.", required=True)
    arg("-j", "--num_workers", type=int, help="Number of CPU threads.", default=12)
    arg("-r", "--checkpoint_path", type=Path, default=None, help="Path to checkout.")
    return parser.parse_args() 
Example 26
Project: fs_image   Author: facebookincubator   File: send_fds_and_run.py    MIT License 5 votes vote down vote up
def parse_opts(argv):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        '--fd', type=int, action='append', default=[],
        help='FDs will be provided to the wrapped process with sequential '
            'numbers starting from 3, in the order they were listed on '
            'the command-line. Repeat to pass multiple FDs.',
    )
    parser.add_argument(
        '--sudo', action='store_true',
        help='Wrap `recv-fds-and-run` with `sudo`, effectively emulating the '
            'behavior of `sudo -C`. See `--sudo-arg` if you need to '
            'pass arguments to `sudo`. Without this option, this CLI is a '
            'very elaborate way of remapping the listed FDs and closing all '
            'others.',
    )
    parser.add_argument(
        '--sudo-arg', action='append', default=[],
        help='Pass this argument to `sudo` on the command-line.'
    )
    parser.add_argument(
        '--no-set-listen-fds', action='store_false', dest='set_listen_fds',
        help='Do not set LISTEN_FDS and LISTEN_PID on the wrapped process. By '
            'default we set these just in case this wraps `systemd-nspawn` -- '
            'that tells it to forward our FDS to the container. If the extra '
            'environment variables are a problem for you, pass this option.',
    )
    parser.add_argument(
        'cmd', nargs='+', help='The command to wrap and supply with FDs.',
    )
    opts = parser.parse_args(argv)
    assert not opts.sudo_arg or opts.sudo, '--sudo-arg requires --sudo'
    return opts 
Example 27
Project: fs_image   Author: facebookincubator   File: recv_fds_and_run.py    MIT License 5 votes vote down vote up
def parse_opts(argv):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        '--unix-sock', required=True,
        help='Connect to the unix socket at this path to receive FDs.',
    )
    parser.add_argument(
        '--num-fds', type=int, required=True,
        help='The number of FDs to inject, from 3 through (3 + NUM_FDS - 1).',
    )
    parser.add_argument(
        '--no-set-listen-fds', action='store_false', dest='set_listen_fds',
        help='Do not set LISTEN_FDS and LISTEN_PID on the wrapped process. By '
            'default we set these just in case this wraps `systemd-nspawn` -- '
            'that tells it to forward our FDS to the container. If the extra '
            'environment variables are a problem for you, pass this option.',
    )
    parser.add_argument(
        'cmd', nargs='+', help='The command to wrap and supply with FDs.',
    )
    return parser.parse_args(argv)


# This cannot be tested as a library since it `exec`s and rewrites the file
# descriptor table.  However, `test_send_fds_and_run.py` covers this fully. 
Example 28
Project: fs_image   Author: facebookincubator   File: cli.py    MIT License 5 votes vote down vote up
def main(argv, from_file: BytesIO, to_file: BytesIO):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    Storage.add_argparse_arg(
        parser, '--storage', required=True,
        help='JSON blob for creating a Storage instance.',
    )
    parser.add_argument('--debug', action='store_true', help='Log more?')
    subparsers = parser.add_subparsers(help='Sub-commands have help.')

    parser_get = subparsers.add_parser('get', help='Download blob to stdout')
    parser_get.add_argument('storage_id', help='String of the form KEY:ID')
    parser_get.set_defaults(to_file=to_file)
    parser_get.set_defaults(func=get)

    parser_put = subparsers.add_parser(
        'put', help='Write a blob from stdin, print its ID to stdout',
    )
    parser_put.set_defaults(from_file=from_file)
    parser_put.set_defaults(to_file=to_file)  # For the storage ID
    parser_put.set_defaults(func=put)

    args = parser.parse_args(argv)
    init_logging(debug=args.debug)

    args.func(args) 
Example 29
Project: fs_image   Author: facebookincubator   File: subvolume_garbage_collector.py    MIT License 5 votes vote down vote up
def parse_args(argv):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        '--refcounts-dir', required=True,
        help='We will create a hardlink to `--new-subvolume-json` in this '
            'directory. For that reason, this needs to be on same device, '
            'and thus cannot be under `--subvolumes-dir`',
    )
    parser.add_argument(
        '--subvolumes-dir', required=True,
        help='A directory on a btrfs volume, where all the subvolume wrapper '
            'directories reside.',
    )
    parser.add_argument(
        '--new-subvolume-wrapper-dir',
        help='Subvolumes live inside wrapper directories, following the '
            'convention <name>:<version>/<name>. This parameter should '
            'consist just of the <name>:<version> part.',
    )
    parser.add_argument(
        '--new-subvolume-json',
        help='We will delete any file at this path, then create an empty one, '
            'and hard-link into `--refcounts-dir` for refcounting purposes. '
            'The image compiler will then write data into this file.',
    )
    return parser.parse_args(argv) 
Example 30
Project: leapp-repository   Author: oamg   File: ntp2chrony.py    Apache License 2.0 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser(description="Convert ntp configuration to chrony.")
    parser.add_argument("-r", "--root", dest="roots", default=["/"], nargs="+",
                        metavar="DIR", help="specify root directory (default /)")
    parser.add_argument("--ntp-conf", action="store", default="/etc/ntp.conf",
                        metavar="FILE", help="specify ntp config (default /etc/ntp.conf)")
    parser.add_argument("--step-tickers", action="store", default="",
                        metavar="FILE", help="specify ntpdate step-tickers config (no default)")
    parser.add_argument("--chrony-conf", action="store", default="/etc/chrony.conf",
                        metavar="FILE", help="specify chrony config (default /etc/chrony.conf)")
    parser.add_argument("--chrony-keys", action="store", default="/etc/chrony.keys",
                        metavar="FILE", help="specify chrony keyfile (default /etc/chrony.keys)")
    parser.add_argument("-b", "--backup", action="store_true", help="backup existing configs before writing")
    parser.add_argument("-L", "--ignored-lines", action="store_true", help="print ignored lines")
    parser.add_argument("-D", "--ignored-directives", action="store_true",
                        help="print names of ignored directives")
    parser.add_argument("-n", "--dry-run", action="store_true", help="don't make any changes")
    parser.add_argument("-v", "--verbose", action="count", default=0, help="increase verbosity")

    args = parser.parse_args()

    logging.basicConfig(format="%(message)s",
                        level=[logging.ERROR, logging.INFO, logging.DEBUG][min(args.verbose, 2)])

    for root in args.roots:
        conf = NtpConfiguration(root, args.ntp_conf, args.step_tickers)

        if args.ignored_lines:
            for line in conf.ignored_lines:
                print(line)

        if args.ignored_directives:
            for directive in conf.ignored_directives:
                print(directive)

        conf.write_chrony_configuration(args.chrony_conf, args.chrony_keys, args.dry_run, args.backup) 
Example 31
Project: mutatest   Author: EvanKepner   File: test_cli.py    MIT License 5 votes vote down vote up
def mock_parser():
    """Mock parser."""
    parser = argparse.ArgumentParser(prog="mock_parser", description=("Mock parser"))
    parser.add_argument("-e", "--exclude", action="append", default=[], help="Append")
    parser.add_argument("-b", "--blacklist", nargs="*", default=[], help="Nargs")
    parser.add_argument("--debug", action="store_true", help="Store True.")

    return parser 
Example 32
Project: incubator-spot   Author: apache   File: collector.py    Apache License 2.0 5 votes vote down vote up
def _parse_args():
    '''
        Parse command-line options found in 'args' (default: sys.argv[1:]).

    :returns: On success, a namedtuple of Values instances.
    '''
    parser   = ArgumentParser('Distributed Collector Daemon of Apache Spot', epilog='END')
    required = parser.add_argument_group('mandatory arguments')

    # .................................state optional arguments
    parser.add_argument('-c', '--config-file',
        default='ingest_conf.json',
        type=file,
        help='path of configuration file',
        metavar='')

    parser.add_argument('-l', '--log-level',
        default='INFO',
        help='determine the level of the logger',
        metavar='')

    parser.add_argument('--skip-conversion',
        action='store_true',
        default=False,
        help='no transformation will be applied to the data; useful for importing CSV files')

    # .................................state mandatory arguments
    required.add_argument('--topic',
        required=True,
        help='name of topic where the messages will be published')

    required.add_argument('-t', '--type',
        choices=pipelines.__all__,
        required=True,
        help='type of data that will be collected')

    return parser.parse_args() 
Example 33
Project: incubator-spot   Author: apache   File: start_oa.py    Apache License 2.0 5 votes vote down vote up
def main():

    # input parameters.
    parser = argparse.ArgumentParser(description="Master OA Script")
    parser.add_argument('-d','--date',required=True,dest='date',help='Date data that will be processed by OA  (i.e 20161102)',metavar='')
    parser.add_argument('-t','--type',required=True,dest='type',help='Data type that will be processed by OA (i.e dns, proxy, flow)',metavar='')
    parser.add_argument('-l','--limit',required=True,dest='limit',help='Num of suspicious connections that will be processed by OA.',metavar='')
    args= parser.parse_args()

    start_oa(args) 
Example 34
Project: cloudflare-ddns   Author: ailionx   File: cloudflare.py    MIT License 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('email')
    parser.add_argument('api_key')
    parser.add_argument('domain')
    parser.add_argument('--proxied', '-p', dest='proxied', action='store_true', default=False,
                        help='Enable the Cloudflare proxy for the record')
    args = parser.parse_args()
    cf = CloudFlare(**vars(args))
    cf.sync_dns_from_my_ip() 
Example 35
Project: meta-transfer-learning   Author: erfaneshrati   File: args.py    MIT License 5 votes vote down vote up
def argument_parser():
    """
    Get an argument parser for a training script.
    """
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--pretrained', help='evaluate a pre-trained model',
                        action='store_true', default=False)
    parser.add_argument('--seed', help='random seed', default=0, type=int)
    parser.add_argument('--checkpoint', help='checkpoint directory', default='model_checkpoint')
    parser.add_argument('--classes', help='number of classes per inner task', default=5, type=int)
    parser.add_argument('--shots', help='number of examples per class', default=5, type=int)
    parser.add_argument('--train-shots', help='shots in a training batch', default=0, type=int)
    parser.add_argument('--inner-batch', help='inner batch size', default=5, type=int)
    parser.add_argument('--inner-iters', help='inner iterations', default=20, type=int)
    parser.add_argument('--replacement', help='sample with replacement', action='store_true')
    parser.add_argument('--learning-rate', help='Adam step size', default=1e-3, type=float)
    parser.add_argument('--meta-step', help='meta-training step size', default=0.1, type=float)
    parser.add_argument('--meta-step-final', help='meta-training step size by the end',
                        default=0.1, type=float)
    parser.add_argument('--meta-batch', help='meta-training batch size', default=1, type=int)
    parser.add_argument('--meta-iters', help='meta-training iterations', default=400000, type=int)
    parser.add_argument('--eval-batch', help='eval inner batch size', default=5, type=int)
    parser.add_argument('--eval-iters', help='eval inner iterations', default=50, type=int)
    parser.add_argument('--eval-samples', help='evaluation samples', default=100, type=int)
    parser.add_argument('--eval-interval', help='train steps per eval', default=1000, type=int)
    parser.add_argument('--weight-decay', help='weight decay rate', default=1, type=float)
    parser.add_argument('--transductive', help='evaluate all samples at once', action='store_true')
    parser.add_argument('--foml', help='use FOML instead of Reptile', action='store_true')
    parser.add_argument('--foml-tail', help='number of shots for the final mini-batch in FOML',
                        default=None, type=int)
    parser.add_argument('--sgd', help='use vanilla SGD instead of Adam', action='store_true')
    parser.add_argument('--metatransfer', help='use meta transfer learning approach', action='store_true')
    parser.add_argument('--gpu', help='gpu id', default=0, type=int)
    return parser 
Example 36
Project: heroku-log-lights   Author: codingjoe   File: __main__.py    MIT License 5 votes vote down vote up
def get_args():
    """Setup argument parser and return parsed arguments."""
    parser = argparse.ArgumentParser(
        description=__doc__.strip()
    )
    parser.add_argument('-a', '--app', dest='app', metavar='HEROKU_APP', type=str,
                        help='Name of the target Heroku app.')
    parser.add_argument('-t', '--token', dest='token', metavar='AUTH_TOKEN', type=str,
                        default=None, help='Heroku AUTH token.')
    parser.add_argument('-v', dest='verbose', action='store_const',
                        const=logging.DEBUG, default=logging.WARNING,
                        help='verbose mode (default: off)')
    return parser.parse_args() 
Example 37
Project: projection-methods   Author: akshayka   File: save_random_cone_program.py    GNU General Public License v3.0 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    # --- input/output --- #
    parser.add_argument(
        'output', metavar='O', type=str,
        help='output path specifying location in which to problem.')
    # --- problem parameters --- #
    parser.add_argument(
        '-cd', '--cone_dims', required=True, type=int, nargs='+',
        help='list of cone dimensions')
    parser.add_argument(
        '-c', '--cones', required=True, type=str, nargs='+',
        help='list of cone classes belonging to %s' % str(k_cones.keys()))
    parser.add_argument(
        '-n', '--n', required=True, type=int,
        help='number of variables (i.e., dimension of p)')
    parser.add_argument(
        '-d', '--density', type=float, default=.01,
        help='density of data matrix A')
    args = parser.parse_args()

    path = check_path(args.output)
    dim = 2 * (sum(args.cone_dims) + args.n + 1)
    x = cvxpy.Variable(dim)

    for c in args.cones:
        assert c in k_cones
    cones = [k_cones[c] for c in args.cones]
    cone_program = random_cone_program(x=x, cone_dims=args.cone_dims,
        cones=cones, n=args.n, density=args.density)
    save_problem(path, cone_program) 
Example 38
Project: cat-bbs   Author: aleju   File: create_dataset.py    MIT License 5 votes vote down vote up
def main():
    """Generate datasets."""

    parser = argparse.ArgumentParser(description="Process some integers.")
    parser.add_argument("--dataset_dir", help="")
    parser.add_argument("--out_dir", default="", help="")
    parser.add_argument("--img_size", default=224, type=int, help="")
    args = parser.parse_args()

    assert args.dataset_dir is not None, "Expected 10k cats dataset directory via --dataset_dir"

    # load images and their facial keypoints
    print("Loading filepaths...")
    fps = find_image_filepaths(args.dataset_dir)

    print("Loading images...")
    examples = []
    for i, fp in enumerate(fps):
        if (i+1) % 100 == 0:
            print("Image %d of %d..." % (i+1, len(fps)))
        img = ndimage.imread(fp, mode="RGB")
        kps = load_keypoints(fp, image_height=img.shape[0], image_width=img.shape[1])
        img_square, (added_top, added_right, added_bottom, added_left) = to_aspect_ratio_add(img, 1.0, return_paddings=True)
        kps = [(x+added_left, y+added_top) for (x, y) in kps]
        #rs_factor = args.img_size / img_square.shape[0]
        img_square_rs = misc.imresize(img_square, (args.img_size, args.img_size))
        kps_rs = [(int((x/img_square.shape[1])*args.img_size), int((y/img_square.shape[0])*args.img_size)) for (x, y) in kps]
        examples.append(Example(fp, img_square_rs, kps_rs))

    # save datasets
    print("Saving pickle file...")
    with open(os.path.join(args.out_dir, "cats-dataset.pkl"), "w") as f:
        pickle.dump(examples, f) 
Example 39
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test(self):

        parser = argparse.ArgumentParser()
        parser.register('action', 'my_action', self.MyAction)
        parser.add_argument('badger', action='my_action')

        self.assertEqual(parser.parse_args(['1']), NS(badger='foo[1]'))
        self.assertEqual(parser.parse_args(['42']), NS(badger='foo[42]'))


# ================
# Subparsers tests
# ================ 
Example 40
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_conflicting_parents(self):
        self.assertRaises(
            argparse.ArgumentError,
            argparse.ArgumentParser,
            parents=[self.w_parent, self.wxyz_parent]) 
Example 41
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_conflicting_parents_mutex(self):
        self.assertRaises(
            argparse.ArgumentError,
            argparse.ArgumentParser,
            parents=[self.abcd_parent, self.ab_mutex_parent]) 
Example 42
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def assertTypeError(self, *args, **kwargs):
        parser = argparse.ArgumentParser()
        self.assertRaises(TypeError, parser.add_argument,
                          *args, **kwargs) 
Example 43
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def assertValueError(self, *args, **kwargs):
        parser = argparse.ArgumentParser()
        self.assertRaises(ValueError, parser.add_argument,
                          *args, **kwargs) 
Example 44
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_invalid_action(self):
        self.assertValueError('-x', action='foo')
        self.assertValueError('foo', action='baz')
        self.assertValueError('--foo', action=('store', 'append'))
        parser = argparse.ArgumentParser()
        try:
            parser.add_argument("--foo", action="store-true")
        except ValueError:
            e = sys.exc_info()[1]
            expected = 'unknown action'
            msg = 'expected %r, found %r' % (expected, e)
            self.assertTrue(expected in str(e), msg) 
Example 45
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_multiple_dest(self):
        parser = argparse.ArgumentParser()
        parser.add_argument(dest='foo')
        try:
            parser.add_argument('bar', dest='baz')
        except ValueError:
            e = sys.exc_info()[1]
            expected = 'dest supplied twice for positional argument'
            msg = 'expected %r, found %r' % (expected, e)
            self.assertTrue(expected in str(e), msg) 
Example 46
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_parsers_action_missing_params(self):
        self.assertTypeError('command', action='parsers')
        self.assertTypeError('command', action='parsers', prog='PROG')
        self.assertTypeError('command', action='parsers',
                             parser_class=argparse.ArgumentParser) 
Example 47
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_dest(self):
        parser = argparse.ArgumentParser()
        action = parser.add_argument('--foo')
        self.assertEqual(action.dest, 'foo')
        action = parser.add_argument('-b', '--bar')
        self.assertEqual(action.dest, 'bar')
        action = parser.add_argument('-x', '-y')
        self.assertEqual(action.dest, 'x') 
Example 48
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_bad_type(self):
        self.assertRaises(ValueError, argparse.ArgumentParser,
                          conflict_handler='foo') 
Example 49
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_conflict_error(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('-x')
        self.assertRaises(argparse.ArgumentError,
                          parser.add_argument, '-x')
        parser.add_argument('--spam')
        self.assertRaises(argparse.ArgumentError,
                          parser.add_argument, '--spam') 
Example 50
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_resolve_error(self):
        get_parser = argparse.ArgumentParser
        parser = get_parser(prog='PROG', conflict_handler='resolve')

        parser.add_argument('-x', help='OLD X')
        parser.add_argument('-x', help='NEW X')
        self.assertEqual(parser.format_help(), textwrap.dedent('''\
            usage: PROG [-h] [-x X]

            optional arguments:
              -h, --help  show this help message and exit
              -x X        NEW X
            '''))

        parser.add_argument('--spam', metavar='OLD_SPAM')
        parser.add_argument('--spam', metavar='NEW_SPAM')
        self.assertEqual(parser.format_help(), textwrap.dedent('''\
            usage: PROG [-h] [-x X] [--spam NEW_SPAM]

            optional arguments:
              -h, --help       show this help message and exit
              -x X             NEW X
              --spam NEW_SPAM
            '''))


# =============================
# Help and Version option tests
# =============================