我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用click.Argument()。
def get_choices(cli, prog_name, args, incomplete): ctx = resolve_ctx(cli, prog_name, args) if ctx is None: return optctx = None if args: for param in ctx.command.get_params(ctx): if isinstance(param, Option) and not param.is_flag and args[-1] in param.opts + param.secondary_opts: optctx = param choices = [] if optctx: choices += [c if isinstance(c, tuple) else (c, None) for c in optctx.type.complete(ctx, incomplete)] elif incomplete and not incomplete[:1].isalnum(): for param in ctx.command.get_params(ctx): if not isinstance(param, Option): continue for opt in param.opts: if startswith(opt, incomplete): choices.append((opt, param.help)) for opt in param.secondary_opts: if startswith(opt, incomplete): # don't put the doc so fish won't group the primary and # and secondary options choices.append((opt, None)) elif isinstance(ctx.command, MultiCommand): for name in ctx.command.list_commands(ctx): if startswith(name, incomplete): choices.append((name, ctx.command.get_command_short_help(ctx, name))) else: for param in ctx.command.get_params(ctx): if isinstance(param, Argument): choices += [c if isinstance(c, tuple) else (c, None) for c in param.type.complete(ctx, incomplete)] for item, help in choices: yield (item, help)
def activate(self): self.commands.register("recipe_list", "Lists all recipes", self._recipe_list) self.commands.register("recipe_build", "Builds a given recipe", self._recipe_build, params=[Argument(("recipe",), required=True)]) self.recipes.register("gw_package", os.path.abspath(os.path.join(os.path.dirname(__file__), "../recipes/gw_package")), description="Groundwork basic package. Includes places for " "apps, plugins, patterns and recipes.", final_words="Recipe Installation is done.\n\n" "During development use buildout:\n" "Run: python bootstrap.py\n" "Then: bin/buildout\n" "Start the app: bin/app\n\n" "For installation run: 'python setup.py install' \n" "For documentation run: 'make html' inside doc folder " "(after installation!)\n\n" "For more information, please take a look into the README file " "to know how to go on.\n" "For help visit: https://groundwork.readthedocs.io\n\n" "Have fun with your groundwork package.")
def generate_cli(spec): origin_url = None if isinstance(spec, str): if spec.startswith('https://') or spec.startswith('http://'): origin_url = spec r = requests.get(spec) r.raise_for_status() spec = yaml.safe_load(r.text) else: with open(spec, 'rb') as fd: spec = yaml.safe_load(fd.read()) spec = sanitize_spec(spec) cli = clickclick.AliasedGroup(context_settings=CONTEXT_SETTINGS) spec = Spec.from_dict(spec, origin_url=origin_url) for res_name, res in spec.resources.items(): grp = clickclick.AliasedGroup(normalize_command_name(res_name), short_help='Manage {}'.format(res_name)) cli.add_command(grp) for op_name, op in res.operations.items(): name = get_command_name(op) cmd = click.Command(name, callback=partial(invoke, op=op), short_help=op.op_spec.get('summary')) for param_name, param in op.params.items(): if param.required: arg = click.Argument([param.name]) cmd.params.append(arg) else: arg = click.Option(['--' + param.name]) cmd.params.append(arg) grp.add_command(cmd) return cli
def metavar(self): """ Metavar as a property, to satisfy the slight brokenness of click.Argument Tracked against Click as an issue: https://github.com/pallets/click/issues/674 """ if self.path_required: return "ENDPOINT_ID:PATH" else: return "ENDPOINT_ID[:PATH]"
def _format_argument(arg): """Format the output of a `click.Argument`.""" yield '.. option:: {}'.format(arg.human_readable_name) yield '' yield _indent('{} argument{}'.format( 'Required' if arg.required else 'Optional', '(s)' if arg.nargs != 1 else ''))
def _format_arguments(ctx): """Format all `click.Argument` for a `click.Command`.""" params = [x for x in ctx.command.params if isinstance(x, click.Argument)] for param in params: for line in _format_argument(param): yield line yield ''
def _format_envvar(param): """Format the envvars of a `click.Option` or `click.Argument`.""" yield '.. envvar:: {}'.format(param.envvar) yield '' if isinstance(param, click.Argument): param_ref = param.human_readable_name else: # if a user has defined an opt with multiple "aliases", always use the # first. For example, if '--foo' or '-f' are possible, use '--foo'. param_ref = param.opts[0] yield _indent('Provide a default for :option:`{}`'.format(param_ref))
def create_cmd(self, cmd_action): option_name = click.Argument(['name'], nargs=-1, required=True) cmd = click.Command( name=cmd_action, params=[option_name], help="{} all node's services".format(cmd_action.capitalize()), callback=self.cmd_callback ) return cmd
def activate(self): """ Activates GwDocumentsInfo by registering: * 2 commands (doc, doc_list) * 1 document (documents_overview) """ self.commands.register("doc_list", "List all documents", self._list_documents) self.commands.register("doc", "Shows the documentation", self._show_documentation) self.commands.register("doc_write", "Stores documents as files ", self._store_documentation, params=[Argument(("path",), required=True), Option(("--html", "-h"), required=False, help="Will output html instead of rst", default=False, is_flag=True), Option(("--overwrite", "-o"), required=False, help="Will overwrite existing files", default=False, is_flag=True), Option(("--quiet", "-q"), required=False, help="Will suppress any user interaction", default=False, is_flag=True) ]) self.documents.register(name="documents_overview", content=documents_content, description="Gives an overview about all registered documents")