Beispiel #1
0
 def register_arguments(c: OptionContext):
     c.add_argument(
         'dice',
         nargs=-1,
         help=
         'The dice (or die) to roll, based on DnD: [count]d{4,6,8,10,12,20,%%}, default=d6'
     )
Beispiel #2
0
    def register_arguments(c: OptionContext):
        c.add_option('--filter',
                     '--filter-file',
                     dest='filter_file',
                     required=True,
                     help='File of filters')

        c.add_option('--sql',
                     '--sqlite-db',
                     '--db',
                     '-d',
                     dest='db',
                     required=True,
                     help='SQLite database to read and update')

        c.add_option('--target-directory',
                     dest='target',
                     required=True,
                     help='Target directory to store unique files')

        c.add_option('--log-file',
                     dest='log_file',
                     required=True,
                     help='Log file for detailed run')

        c.add_option('--dry-run',
                     '-n',
                     dest='dry_run',
                     is_flag=True,
                     help='Do change file system, just print what would do')
Beispiel #3
0
 def register_arguments(c: OptionContext) -> None:
     GerritChangeReviewer.register_arguments(c)
     # register own arguments
     c.add_option('-r',
                  '--repo',
                  'gerrit_review_local_repo',
                  help='Conflicts with parent command, should work')
Beispiel #4
0
    def register_arguments(c: OptionContext):
        c.add_option(
            '--filter', '--filter-file', dest='filter_file', required=True,
            help='File of filters')

        c.add_option(
            '--sql', '--sqlite-db', '--db', '-d', dest='db', required=True,
            help='SQLite database to read')
 def test_deep_hierarchy_keeps_different_subgroup_invariant(self):
     tested = OptionContext().add_multilevel_group(
     ).add_mutually_exclusive_group()
     self.assert_true(hasattr(tested, 'add_group'))
     self.assert_false(hasattr(tested, 'add_mutually_exclusive_group'))
     tested = OptionContext().add_mutually_exclusive_group().add_group()
     self.assert_false(hasattr(tested, 'add_group'))
     self.assert_true(hasattr(tested, 'add_mutually_exclusive_group'))
Beispiel #6
0
 def register_arguments(c: OptionContext):
     c.add_option('-i',
                  '--input',
                  required=True,
                  help='The CSV file to process')
     c.add_option('-o',
                  '--output',
                  required=False,
                  help='The output CSV filename')
Beispiel #7
0
 def register_directory_args(c: OptionContext):
     c.add_option(
         '-d', '--directory', '--source-directory', dest='directory', required=True,
         help='The local root directory which contains files to be synced'
     )
     c.add_option(
         '-t', '--target-directory', dest='target_directory', required=True,
         help='The target root directory, everything will be synced below this directory'
     )
Beispiel #8
0
 def register_arguments(c: OptionContext):
     c.add_option('-p',
                  '--port',
                  type=int,
                  default=8081,
                  help='The port number, default: 8081')
     c.add_option(
         '--count',
         default='40M',
         help='Amount of bytes. Can be used the K/M/G suffix. Default: 40M')
Beispiel #9
0
    def register_sync_entries(c: OptionContext):
        c.add_option(
            '-e', '--entry', required=True, multiple=True,
            help='A file sync entry describing what to synchronize in format'
                 ' source;target;permissions;owner;group;flags'
        )

        c.add_option(
            '-n', '--no-chmod-chown', '--skip-chmod', dest='skip_chmod', is_flag=True, default=False,
            help='Always skip chmod and chown (to run as non-admin on target FS)'
        )
Beispiel #10
0
 def set_up(self):
     self.tested = OptionContext()
     db = self.tested.add_mutually_exclusive_group('Database settings',
                                                   required=True)
     db.add_option('--config-file')
     settings = db.add_group(require_all=True)
     pw = settings.add_mutually_exclusive_group()
     pw.add_option('--password')
     pw.add_option('--password-file')
     settings.add_option('--host')
     settings.add_option('--port', type=int)
     self.params = {}
Beispiel #11
0
 def register_arguments(c: OptionContext):
     c.add_option('-s',
                  '--smaller-than',
                  dest='smaller_than',
                  type=int,
                  help='Print till the primes are smaller than this value.')
     c.add_option(
         '-c',
         '--count',
         type=int,
         help=
         'Max number of printed primes (if --smaller-than is not specified, count is 100)'
     )
Beispiel #12
0
    def register_arguments(c: OptionContext):
        licenses = c.add_mutually_exclusive_group('License Types',
                                                  required=True)
        for name, text in LicenseChange.LICENSES.items():
            licenses.add_option(f'--{name}',
                                dest='license_type',
                                flag_value=name,
                                help=f'Switches to {text}')

        c.add_argument('targets',
                       nargs=-1,
                       required=True,
                       help='One or more directory or file to be updated')
Beispiel #13
0
    def register_arguments(c: OptionContext):
        c.add_option('--wait-for-close', help='Wait a minute before closing Chrome', is_flag=True)

        driver_grp = c.add_group(
            'Browser and WebDriver options',
            help='Options influences the behaviour of Selenium Web Driver and Google Chrome / Firefox')

        driver_grp.add_option('--screenshots', '--screenshot-dir', dest='screenshot_dir', default='.',
                                help='The directory to save any screenshot, default: current directory')
        driver_grp.add_option('--download-directory', '--dl-dir', dest='download_dir', required=True,
                                help='Download directory')
        driver_grp.add_option('--headless', is_flag=True, help='Start Chrome in headless mode')
        driver_grp.add_option('--timeout', type=int, default=60,
                                help='Timeout for waiting any element or action, default: 60s')
Beispiel #14
0
    def _invoke(self,
                optctx: OptionContext,
                callback: callable,
                args,
                *,
                expected_exit_value=1):
        @click.pass_context
        def r(cctx: click.Context, *_args, **_kwargs):
            sys.exit(callback(cctx.params) or 0)

        optctx.add_args_to_func(r)
        r = click.command(self.APP_NAME)(r)
        with self.assert_raises(SystemExit) as context:
            r(args, self.APP_NAME)

        self.assert_equal(expected_exit_value, context.exception.code)
Beispiel #15
0
 def register_arguments(c: OptionContext):
     c.add_option(
         '-s',
         '--source',
         '--source-directory',
         dest='source_dir',
         required=True,
         help='Source Directory of files (photos) to be checked and cleaned'
     )
     c.add_option('--sql',
                  '--sqlite-db',
                  '--db',
                  '-d',
                  dest='db',
                  required=True,
                  help='SQLite database to store data')
Beispiel #16
0
    def _register_subcommands(self, command_classes, parent_run_cmd,
                              app_context: ApplicationContext):
        for command_class in command_classes:

            def wrapper():
                c = command_class

                @click.pass_context
                def r(ctx: click.Context, *_, **kwargs):
                    app_context.add_cmd_args(ctx.info_name, ctx.params)
                    app_context.command_names.current = get_invoked_subommand(
                        ctx.parent)
                    app_context.command_names.invoked_subcommand = get_invoked_subommand(
                        ctx)
                    app_context.command_names.invoked_subcommand_primary_name = ctx.invoked_subcommand
                    app_context.current_args = Node.create_from(ctx.params)
                    for k, v in kwargs.items():
                        ctx.obj.add_arg(k, v)

                    log_debug('Starting command', name=c.name)
                    res = c().run(ctx.obj)
                    if ctx.invoked_subcommand is None or res:
                        ctx.exit(res)

                r.command_class = c

                return r

            try:
                cmd_opt_ctx = OptionContext()
                cmd_opt_ctx.register_args(command_class.register_arguments)
                cls = AliasedGroup if command_class.subcommand_classes else click.core.Command
                cmd_run = parent_run_cmd.command(
                    name=command_class.name,
                    help=command_class.description,
                    cls=cls,
                    context_settings=CONTEXT_SETTINGS)(
                        cmd_opt_ctx.add_args_to_func(wrapper()))
                self._register_subcommands(command_class.subcommand_classes,
                                           cmd_run, app_context)
            except Exception as e:
                print(
                    f'Error occurred while registering args for command `{command_class.name}`: {e}'
                )
                raise
Beispiel #17
0
    def register_arguments(c: OptionContext):
        c.add_option(
            '-v',
            '--verbose',
            is_flag=True,
            default=False,
            dest='verbose',
            help='Verbose output, prints the details of hashed values')

        grp = c.add_mutually_exclusive_group(required=True)
        grp.add_option(
            '--phash',
            '--python-hash',
            dest='phash',
            metavar='PYTHON-DIR',
            help=
            'Calculate MD5 hex digest of a directory without .git and __pychache__'
        )
Beispiel #18
0
def register_global_args_in_review_cmd(c: OptionContext) -> None:
    """
    Register global args not listed in the subcommands but in parent ReviewCommand.

    Added here just to be always visible in this module.
    """
    c.add_option('-U',
                 '--gerrit-ssh-user',
                 'gerrit_ssh_user',
                 help='SSH username to send review into gerrit')
    c.add_option(
        '--upload-gerrit-review',
        '--review',
        'upload_gerrit_review',
        is_flag=True,
        help=
        'Upload gerrit review. By default the code uses dry run and print the review instead.'
    )

    c.add_option('-R',
                 '--repo',
                 'gerrit_review_local_repo',
                 required=True,
                 help='Location of the bare git repository. '
                 'Can be an existing repo or a non-existent directory.')
Beispiel #19
0
 def register_arguments(c: OptionContext):
     c.add_option(
         '-d',
         '--directory',
         '--target-directory',
         dest='directory',
         required=True,
         help=
         'An empty or non-existant directory where the splitted logs will be'
     )
     c.add_option(
         '--reopen',
         dest='reopen',
         help=
         'Reopen each file when appending to it instead of store handles.'
         ' It works with thousands of sessions but it is very slow')
     c.add_option('-s',
                  '--silent',
                  dest='silent',
                  is_flag=True,
                  help='Do not print status, the line numbers')
     c.add_option(
         '-D',
         '-m',
         '--delimiter',
         dest='delimiter',
         default='.' if sys.platform == 'win32' else ':',
         help=
         'Delimiter character used in filename, default: same as in session_id, the colon'
     )
     c.add_argument(
         'zorplogfile',
         nargs=1,
         default='-',
         help=
         'The original Zorp log file to be splitted. Omit or use - to read from stdin'
     )
Beispiel #20
0
 def register_arguments(c: OptionContext):
     c.add_option(
         '-s', '--source', '--source-directory', dest='source_dir', required=True,
         help='Source Directory of files (photos) to be checked and cleaned')
     c.add_option(
         '-d', '--destination', '--destination-directory', dest='dest_dir', required=True,
         help='Target Root Directory - Files will be copied to $TARGET/$YEAR/$YEAR-$MONTH/$YEAR-$MONTH-$DAY')
     c.add_option(
         '--pretend', dest='pretend', default=False, is_flag=True,
         help='Pretend only, but do not move files')
Beispiel #21
0
    def register_arguments(c: OptionContext):
        c.add_option(
            '--sql', '--sqlite-db', '--db', '-d', dest='db', required=True,
            help='SQLite database to read and update')

        c.add_option('--log-file', dest='log_file', required=True,
                     help='Log file for detailed run')

        c.add_option('--dry-run', '-n', dest='dry_run', is_flag=True,
                     help='Do change file system, just print what would do')
Beispiel #22
0
    def _register_ssh_args(c: OptionContext):
        c.add_option(
            '-H', '--host', required=True,
            help='The SSH server\'s host name or IP address'
        )
        c.add_option(
            '-p', '--port', type=int, default=22,
            help='The SSH sever port, default is 22'
        )

        c.add_option(
            '-l', '--login', '-u', '--user', dest='user', required=True,
            help='The username used on the SSH server'
        )

        c.add_option(
            '--skip-host-key-check', is_flag=True, default=False,
            help='Skip check SSH host key - it is insecure, but in trusted environment it is reasonable'
        )
Beispiel #23
0
 def register_arguments(c: OptionContext):
     c.add_option('--gerrit-url',
                  dest='gerrit_url',
                  required=True,
                  help='Gerrit URL without revision ID')
     c.add_option(
         '--change-id',
         'change_id',
         type=int,
         help='Change ID, should be the same as the end of the gerrit URL')
     c.add_option('--gerrit-revision',
                  '--revision',
                  'revision',
                  type=int,
                  required=True,
                  help='Revision of the change in gerrit url')
Beispiel #24
0
 def register_arguments(c: OptionContext):
     c.add_argument('file_list',
                    nargs=-1,
                    type=click.Path(),
                    help='Files to open')
Beispiel #25
0
 def register_arguments(c: OptionContext):
     c.add_argument('server', help='The SSH server address to log in to')
Beispiel #26
0
 def register_arguments(c: OptionContext) -> None:
     c.add_argument('arguments', nargs=-1)
Beispiel #27
0
 def register_arguments(c: OptionContext):
     c.add_argument(
         'directories', nargs=-1,
         help='The search directories')
Beispiel #28
0
    def register_arguments(c: OptionContext):
        c.add_option('--reprocess', '-f', '--force', is_flag=True, dest='reprocess', default=False,
                     help='Reprocess sysinfo if generated YAML file exists')
        c.add_option('--output', '-o', dest='output_dir', required=True,
                     help='Output directory for result.yaml and index.html')
        c.add_option('--munin-dir', '-m', dest='munin_dir', default='/var/lib/munin',
                     help='Munin directory, may not exist, default: /var/lib/munin')
        c.add_option('--log-dir', '-l', dest='log_dir', default='/var/log',
                     help='Log directory, may not exist, default: /var/log')

        c.add_option('--no-logs', '-L', is_flag=True, dest='no_log', default=False,
                     help='Skip processing logs')
        c.add_option('--no-graphs', '-G', is_flag=True, dest='no_graph', default=False,
                     help='Skip generating munin graphs')
Beispiel #29
0
    def _register_app_args(self, h: OptionContext):
        if self._version:
            h.add_custom_decorator(lambda: click.version_option(self._version))

        h.add_option('--cwd', dest='cwd', help='Change to specified directory')
        h.add_option('--wait',
                     is_flag=True,
                     help='Wait for user input before terminating application')
        h.add_option('--print-backtraces',
                     dest='print_backtraces',
                     is_flag=True,
                     help='Print backtraces of the exceptions')

        debug_opts = ['--debug']
        if self._enable_short_debug_option:
            debug_opts.insert(0, '-d')
        h.add_option(*debug_opts,
                     dest='debug',
                     is_flag=True,
                     help='Enable print/log debug messages')
        if self._enable_env_options:
            h.add_option(
                '-e',
                '--environment',
                dest='environment',
                help=f'Specifies the environment to run the app '
                f'under ({"/".join(sorted(self._env_config.available_envs))})')

        logging = h.add_group('Logging')
        logging.add_option('--log-level',
                           dest='log_level',
                           help='Set log level, default: warning',
                           type=click.Choice(
                               [i.name.lower() for i in LogLevel]),
                           default='info')
        logging.add_option(
            '--log-syslog',
            dest='log_syslog',
            is_flag=True,
            help='Log to syslog. Can be combined with other log targets')
        logging.add_option(
            '--log-console',
            '--log-stdout',
            dest='log_console',
            is_flag=True,
            help=
            'Log to STDOUT, the console. Can be combined with other targets.'
            'If no target is specified, this is used as default.')
        logging.add_option(
            '--log-file',
            dest='log_file',
            multiple=True,
            help='Log to a file. Can be specified multiple times and '
            'can be combined with other options.')
        logging.add_option(
            '--no-log',
            '-l',
            dest='log_none',
            is_flag=True,
            help='Disable logging. If this is set, other targets are invalid.')
Beispiel #30
0
    def run(self, args: typing.Optional[typing.List[str]] = None):
        single_command_mode = bool(self._command_class
                                   and len(self._command_classes) == 1)

        app_context = ApplicationContext()
        app_context.single_command_mode = single_command_mode
        app_context.config_directories = list(
            self._config_dir_registry.config_directories)
        app_context.environment = self._env_config.current_env

        @click.pass_context
        def app_run(ctx: click.Context, *args, **kwargs):
            app_context.add_cmd_args(
                '__main__', ctx.params,
                self._command_class.name if single_command_mode else '')
            app_context.command_names.invoked_subcommand = get_invoked_subommand(
                ctx)
            app_context.command_names.invoked_subcommand_primary_name = ctx.invoked_subcommand
            app_context.current_args = Node.create_from(ctx.params)
            ctx.obj = app_context
            ctx.obj.command_registry = self._command_registry
            ctx.obj.program_name = self._program_name
            for k, v in kwargs.items():
                ctx.obj.add_arg(k, v)
            self._process_debug_opts(ctx.obj.args)
            if self._process_logging_options(ctx.obj.args):
                sys.exit(1)

            if self._enable_env_options and ctx.obj.args.environment:
                self._env_config.set_current_env(ctx.obj.args.environment)

            self._load_env()

            if single_command_mode:
                app_context.command_names.current = self._command_class.name
                res = self._command_class().run(app_context)
                if ctx.invoked_subcommand is None or res:
                    ctx.exit(res)

        app_run.app_context = app_context

        app_opt_ctx = OptionContext()
        app_opt_ctx.register_args(self._register_app_args)
        if single_command_mode:
            app_opt_ctx.register_args(self._command_class.register_arguments)

        try:
            app_run = app_opt_ctx.add_args_to_func(app_run)
        except Exception as e:
            if single_command_mode:
                suffix = f'for command `{self._command_class.name}`'
            else:
                suffix = 'for the application'
            print(f'Error occurred while registering args {suffix}: {e}')
            raise

        if single_command_mode:
            has_classes = len(self._command_class.subcommand_classes) > 0
            app_run = (click.group if has_classes else click.command)(
                self._program_name,
                help=self._description or self._command_class.description,
                context_settings=CONTEXT_SETTINGS,
                cls=(AppGroup if has_classes else AppCommand))(app_run)
            self._register_subcommands(self._command_class.subcommand_classes,
                                       app_run, app_context)
        else:
            self._command_registry.register_class(_ListAllCommand)
            self._command_registry.register_class(_ListCommand)

            app_run = click.group(
                self._program_name,
                cls=AppGroup,
                help=self._description,
                context_settings=CONTEXT_SETTINGS,
            )(app_run)
            self._register_subcommands(self._command_registry._command_classes,
                                       app_run, app_context)

        return app_run(args, self._program_name)