我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用click.command()。
def _run_list_cmd(project, image_name, project_settings): """Run `scrapy list` command inside the image container.""" client = utils.get_docker_client() # FIXME we should pass some value for SCRAPY_PROJECT_ID anyway # to handle `scrapy list` cmd properly via sh_scrapy entrypoint project = str(project) if project else '' job_settings = json.dumps(project_settings) container = client.create_container( image=image_name, command=['list-spiders'], environment={'SCRAPY_PROJECT_ID': project, 'JOB_SETTINGS': job_settings}) if 'Id' not in container: raise shub_exceptions.ShubException( "Create container error:\n %s" % container) client.start(container) statuscode = client.wait(container=container['Id']) return statuscode, client.logs( container=container['Id'], stdout=True, stderr=True if statuscode else False, stream=False, timestamps=False)
def list_commands(self, ctx): self._load_plugin_commands() # The commands available is the list of both the application (if # available) plus the builtin commands. rv = set(click.Group.list_commands(self, ctx)) info = ctx.ensure_object(ScriptInfo) try: rv.update(info.load_app().cli.list_commands(ctx)) except Exception: # Here we intentionally swallow all exceptions as we don't # want the help page to break if the app does not exist. # If someone attempts to use the command we try to create # the app again and this will give us the error. pass return sorted(rv)
def main(as_module=False): this_module = __package__ + '.cli' args = sys.argv[1:] if as_module: if sys.version_info >= (2, 7): name = 'python -m ' + this_module.rsplit('.', 1)[0] else: name = 'python -m ' + this_module # This module is always executed as "python -m flask.run" and as such # we need to ensure that we restore the actual command line so that # the reloader can properly operate. sys.argv = ['-m', this_module] + sys.argv[1:] else: name = None cli.main(args=args, prog_name=name)
def parseSourceDirectory(self): """Parse source directory and do some basic sanity checks.""" sourceDir = self.globalCfg.currentDir if 'local_folder' in self.host_toml or 'source_folder' in self.host_toml: if 'source_folder' in self.host_toml: sourceDirUntested = self.host_toml['source_folder'] else: print("Warning: Key `local_folder` is deprecated. Please use `source_folder`!\nThe following command will in-place modify the file:\n\tsed -i -- 's/local_folder/source_folder/g' {}".format(self.globalCfg.configFilename)) sourceDirUntested = self.host_toml['local_folder'] if not (os.path.isdir(sourceDirUntested) and os.path.exists(sourceDirUntested)): print("You specified the source folder {} to be synced. This folder does not exist!".format(sourceDirUntested)) exit(6) sourceDir = sourceDirUntested if self.verbose: print("# Running with explicit source folder {}".format(sourceDir)) if self.verbose: print("# Using source folder {}".format(sourceDir)) self.localDir = sourceDir
def get_help_recursive(group, ctx, commands): """ Returns help for arbitrarily nested subcommands of the given click.Group. """ try: command_name = commands.pop(0) group = group.get_command(ctx, command_name) if not group: raise click.ClickException('Invalid command: {}'.format(command_name)) except IndexError: # end of subcommand chain return group.get_help(ctx) except AttributeError: # group is actually a command with no children return group.get_help(ctx) return get_help_recursive(group, ctx, commands)
def run(quiet, args): """Run a local command. Examples: $ django run manage.py runserver ... """ if not args: raise ClickException('pass a command to run') cmd = ' '.join(args) application = get_current_application() name = application.name return application.run( cmd, verbose=not quiet, abort=False, capture=True, env={ 'DJANGO_SETTINGS_MODULE': '%s.settings' % name } )
def worker(config_uri): """ Console entry script that starts a worker process """ # TODO: import spacy's model to share it between workers pyramid_env = bootstrap(config_uri) # this conflicts with normal worker output # TODO: solve logging for the console # Setup logging to allow log output from command methods # from pyramid.paster import setup_logging # setup_logging(config_uri) try: qs = ['default'] conn = redis_connection() with Connection(conn): w = Worker(qs) w.work() finally: pyramid_env['closer']()
def _generate_intervals(self): """ Export TimeWarrior data as JSON and calculate bins. """ assert len(self.time_span.split(' ')) >= 1 if len(self.time_span.split(' ')) == 1: command = [':{d}'.format(d=self.time_span)] elif len(self.time_span.split(' ')) > 1: command = self.time_span.split(' ') process = subprocess.Popen( ['timew', 'export'] + command + [self.tag], stdout=subprocess.PIPE ) out, err = process.communicate() intervals = json.loads(out.decode('utf8').replace('\n', '')) if intervals and (not intervals[-1].get('end')): del intervals[-1] return intervals
def attach(app, container, host, command): """ Attaches to a container """ if command: shell = ['/bin/bash', '-lc', ' '.join(command)] else: shell = ['/bin/bash'] # See if the container is running formation = FormationIntrospector(host, app.containers).introspect() for instance in formation: if instance.container == container: # Work out anything to put before the shell (e.g. ENV) pre_args = [] if os.environ.get("TERM", None): pre_args = ["env", "TERM=%s" % os.environ['TERM']] # Launch into an attached shell status_code = subprocess.call(["docker", "exec", "-it", instance.name] + pre_args + shell) sys.exit(status_code) # It's not running ;( click.echo(RED("Container {name} is not running. It must be started to attach - try `bay run {name}`.".format( name=container.name, )))
def help(ctx, command_name): """ Shows main command list. """ from ..cli import cli # Find subcommand if command_name: subcommand = cli.get_command(None, command_name) if subcommand is None: click.echo(RED("There is no command {}".format(command_name))) sys.exit(1) else: # Override info name so help prints correctly ctx.info_name = subcommand.name click.echo(subcommand.get_help(ctx)) # Print main help else: ctx.info_name = "bay" ctx.parent = None click.echo(cli.get_help(ctx))
def shell(app, container, host, command): """ Runs a single container with foreground enabled and overridden to use bash. """ # Get the current formation formation = FormationIntrospector(host, app.containers).introspect() # Make a Formation with that container launched with bash in foreground try: instance = formation.add_container(container, host) except ImageNotFoundException as e: click.echo(RED(str(e))) sys.exit(1) instance.foreground = True if command: instance.command = ['/bin/bash -lc "{}"'.format(' '.join(command))] else: instance.command = ["/bin/bash -l"] # Run that change task = Task("Shelling into {}".format(container.name), parent=app.root_task) run_formation(app, host, formation, task)
def test_option_with_multiple_type_yes_default(): @click.command() @option('--my-option', '-mo', {'multiple': True, 'type': int}, "a click option") def f(my_option=[2, 3]): click.echo(repr(my_option)) runner = CliRunner() # Check that it will run with default result = runner.invoke(f, []) assert result.exception is None assert result.output.strip() == '(2, 3)' # Check that it will run normally result = runner.invoke(f, ['-mo', '3', '-mo', '4']) assert result.exception is None assert result.output.strip() == '(3, 4)'
def is_incomplete_source(self, src, filename="<input>", symbol="single"): """ Test if a given source code is incomplete. Incomplete code may appear in users interactions when user is typing a multi line command: for x in range(10): ... should continue here, but user already pressed enter! """ try: pytuga_src = self.transpile(src) except SyntaxError: return True return codeop.compile_command(pytuga_src, filename, symbol) is None
def potfile(): """ Updates Transpyler lib main potfile. You probably has little use for this command unless you are a Transpyler developer. """ from transpyler.translate import L10N_PATH from transpyler.utils import collect_mod_namespace from transpyler.translate import extract_translations from transpyler.translate import create_pot_file click.echo('Updating transpyler.pot file...', nl=False) path = os.path.join(L10N_PATH, 'transpyler.pot') names = collect_mod_namespace() translations = extract_translations(names) create_pot_file(translations, path) click.echo(' Done!')
def get_command(self, ctx, name): try: mod = __import__(f"iocage.cli.{name}", None, None, ["cli"]) mod_name = mod.__name__.replace("iocage.cli.", "") try: if mod.__rootcmd__ and "--help" not in sys.argv[1:]: if len(sys.argv) != 1: if os.geteuid() != 0: sys.exit("You need to have root privileges to" f" run {mod_name}") except AttributeError: # It's not a root required command. pass return mod.cli except (ImportError, AttributeError): return
def logs(config): pod = subprocess.check_output('kubectl get pods -l "app=api" -o name', shell=True) if pod == "": print "There is no pod running in this environment. You may need to deploy first." exit(1) pod_name = pod.split("/")[1].strip() subprocess.call("kubectl logs {pod} --container flask --follow".format(pod=pod_name), shell=True) # @click.command() # @click.argument("config", type=StarterkitConfig(r'kube/deployments/')) # def request_certs(): # # Request certs # subprocess.call("kubectl exec ") # # Get status of Certs # subprocess.call("kubectl") # # If # pass
def _echo_citations(): import q2cli.cache click.secho('\nCitations', fg='green') click.secho('QIIME 2 framework and command line interface', fg='cyan') click.secho('Pending a QIIME 2 publication, please cite QIIME using the ' 'original publication: ' 'http://www.ncbi.nlm.nih.gov/pubmed/20383131') plugins = q2cli.cache.CACHE.plugins if plugins: for name, plugin in sorted(plugins.items()): click.secho('\n%s %s' % (name, plugin['version']), fg='cyan') click.secho(plugin['citation_text']) else: click.secho('\nNo plugins are currently installed.\nYou can browse ' 'the official QIIME 2 plugins at https://qiime2.org')
def __init__(self, component, server_cls, help): """Constructor. :param component: The component to run. :type component: Component :param server_cls: Class for the component server. :param server_cls: ComponentServer :param help: Help text for the CLI command. :type help: str """ self.__startup_callback = None self.__shutdown_callback = None self.__error_callback = None self._args = {} self.component = component self.source_file = None self.callbacks = None self.server_cls = server_cls self.help = help
def cm(port, timezone, profiling, database_uri, attacks_folder, planners_folder): """ Chaos Monkey Engine command line utility """ with profile_ctx(profiling): log = logging.getLogger(__name__) configure_engine(database_uri, attacks_folder, planners_folder, timezone) log.info("Engine configured") log.debug("database: %s", database_uri) log.debug("attacks folder: %s", attacks_folder) log.debug("planners folder: %s", planners_folder) log.debug("timezone: %s", timezone) try: # Catch SIGTERM and convert it to a SystemExit signal.signal(signal.SIGTERM, sigterm_handler) log.info("Serving API at port %s", port) run_api_server(flask_app, port) except (KeyboardInterrupt, SystemExit): shutdown_engine()
def delete_monitor(ctx, monitor, confirm): if not confirm: confirm = click.prompt(''' ! WARNING: Destructive Action ! This command will destroy the monitor: {monitor} ! To proceed, type "{monitor}" or re-run this command with --confirm={monitor} '''.format(monitor=monitor), prompt_suffix='> ') if confirm.strip() != monitor: print('abort') sys.exit(1) with Spinner('Deleting monitor {}: '.format(monitor), remove_message=False): newrelic.delete_monitor(ctx.obj['ACCOUNT'], monitor) print(click.style(u'OK', fg='green', bold=True))
def main(ctx, json): """Manage payment channels. The `21 channels` command is used for creating, opening, closing, and conducting diagnostics for the payment channel micropayments protocol. After opening a channel with a merchant, making a payment returns a token, which the merchant will accept as proof of payment within the 402 payments protocol. Example of opening a channel, making payments, and closing the channel: $ channels open https://mkt.21.co/21dotco/payments/channel 100000 120\n $ channels pay https://mkt.21.co/21dotco/payments/channel 100\n $ channels pay https://mkt.21.co/21dotco/payments/channel 100\n $ channels pay https://mkt.21.co/21dotco/payments/channel 100\n $ channels info https://mkt.21.co/21dotco/payments/channel\n $ channels close https://mkt.21.co/21dotco/payments/channel\n """ client = PaymentChannelClient(Wallet(WALLET_PATH), CHANNELS_DB_PATH) ctx.obj = {'client': client, 'json': json}
def rate(ctx, list, app_id, rating): """Rate an app listed in the 21 Marketplace. \b Usage _____ Rate an app. $ 21 rate Xe8 3 - Xe8 is the id of the app that you want to rate. This id can be found with `21 search`. - 3 is the rating to apply. A rating should be an integer between 1-5. You can update the rating for an app at anytime with `21 rate`. \b List all the apps that you have rated. $ 21 rate --list """ # pylint: disable=redefined-builtin if list: _list(ctx.obj["client"]) else: if not (app_id and isinstance(rating, int)): # print help and exit logger.info(ctx.command.help) return _rate(ctx.obj["client"], app_id, rating)
def buy(ctx, resource, **options): """Buy API calls with bitcoin. \b Usage ----- Buy a bitcoin-payable resource. $ 21 buy <resource> \b Get state, city, latitude, longitude, and estimated population for a given zip code. $ 21 buy "https://mkt.21.co/21dotco/zip_code_data/zipdata/collect?zip_code=94109" --maxprice 2750 """ buy_url = resource if buy_url is None or buy_url is "": logger.info(ctx.command.get_help(ctx)) sys.exit() options['mock_requests'] = ctx.parent.params['mock_requests'] _buy(ctx.obj['config'], ctx.obj['client'], ctx.obj['machine_auth'], buy_url, **options)
def nsupdate_cmd(name, domain, host_records): """ Create nsupdate command line. """ pathname = "%s.%s." % (name, domain) cmd = "server %s\n" % (DNS_SERVER1,) cmd += "zone %s.\n" % (domain,) cmd += "update delete %s\n" % (pathname,) for rec in host_records: cmd += "update add %s %d %s %s\n" % (pathname, rec[4], rec[2], rec[3]) cmd += "show\n" cmd += "send\n" return cmd.encode('utf-8')
def test_display_task(self): @click.command() @pass_ovh def cli(ovh): ovh.display_task({'function': 'foo', 'status': 'init'}) ovh.display_task({'function': 'foo', 'status': 'todo'}) ovh.display_task({'function': 'foo', 'status': 'doing'}) ovh.display_task({'function': 'foo', 'status': 'done'}) ovh.display_task({'function': 'foo', 'status': 'cancelled'}) ovh.display_task({'function': 'foo', 'status': 'bar'}) result = self.runner.invoke(cli) self.assertEqual(result.output, """\ [*] The task foo has been launched. [*] The task foo has been launched. [*] The task foo has been launched. [*] The task foo is done. [warning] The task foo has been cancelled. [error] The task foo fell in an error state. """)
def test_param_with_default__uses_param_default_when_missing(self, cli_runner_isolated): assert ConfigFileProcessor1.config_files[0] == "hello.ini" CONFIG_FILE_CONTENTS = """ [hello] name = Alice """ write_configfile_with_contents("hello.ini", CONFIG_FILE_CONTENTS) assert os.path.exists("hello.ini") CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config()) @click.command(context_settings=CONTEXT_SETTINGS) @click.option("-n", "--name", type=str, default="__CMDLINE__") @click.option("--number", default=123) def hello_with_param_default(name, number): click.echo("param: number= %s" % number) result = cli_runner_isolated.invoke(hello_with_param_default) assert result.output == "param: number= 42\n" assert result.exit_code == 0
def test_param_with_default__uses_cmdline_when_provided(self, cli_runner_isolated): assert ConfigFileProcessor1.config_files[0] == "hello.ini" CONFIG_FILE_CONTENTS = """ [hello] name = Alice number = 43 """ write_configfile_with_contents("hello.ini", CONFIG_FILE_CONTENTS) assert os.path.exists("hello.ini") CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config()) @click.command(context_settings=CONTEXT_SETTINGS) @click.option("-n", "--name", type=str, default="__CMDLINE__") @click.option("--number", default=123) def hello_with_param_default2(name, number): click.echo("param: number= %s" % number) result = cli_runner_isolated.invoke(hello_with_param_default2, ["--number", "234"]) assert result.output == "param: number= 234\n" assert result.exit_code == 0
def test_param_without_default__uses_cmdline_default_when_missing(self, cli_runner_isolated): assert ConfigFileProcessor1.config_files[0] == "hello.ini" CONFIG_FILE_CONTENTS = """ [hello] number = 1234 """ write_configfile_with_contents("hello.ini", CONFIG_FILE_CONTENTS) assert os.path.exists("hello.ini") CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config()) @click.command(context_settings=CONTEXT_SETTINGS) @click.option("-n", "--name", type=str, default="__CMDLINE__") def hello_with_config(name): click.echo("param: name= %s" % name) result = cli_runner_isolated.invoke(hello_with_config) assert result.output == "param: name= __CMDLINE__\n" assert result.exit_code == 0
def test_with_cmdline_and_configfile__prefers_cmdline(self, cli_runner_isolated): assert ConfigFileProcessor1.config_files[0] == "hello.ini" CONFIG_FILE_CONTENTS1 = """ [hello] name = Alice """ write_configfile_with_contents("hello.ini", CONFIG_FILE_CONTENTS1) assert os.path.exists("hello.ini") assert not os.path.exists("hello.cfg") CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config()) @click.command(context_settings=CONTEXT_SETTINGS) @click.option("-n", "--name", default="__CMDLINE__") def hello(name): click.echo("Hello %s" % name) result = cli_runner_isolated.invoke(hello, ["--name", "CMDLINE_VALUE"]) assert result.output == "Hello CMDLINE_VALUE\n" assert result.exit_code == 0
def test_with_configfile2__usable_as_alternative(self, cli_runner_isolated): assert ConfigFileProcessor1.config_files[1] == "hello.cfg" CONFIG_FILE_CONTENTS2 = """ [hello] name = Bob """ write_configfile_with_contents("hello.cfg", CONFIG_FILE_CONTENTS2) assert not os.path.exists("hello.ini") assert os.path.exists("hello.cfg") CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config()) @click.command(context_settings=CONTEXT_SETTINGS) @click.option("-n", "--name", default="__CMDLINE__") def hello(name): click.echo("Hello %s" % name) result = cli_runner_isolated.invoke(hello) assert result.output == "Hello Bob\n" assert result.exit_code == 0
def test_with_configfile12__prefers_configfile1(self, cli_runner_isolated): assert ConfigFileProcessor1.config_files == ["hello.ini", "hello.cfg"] CONFIG_FILE_CONTENTS1 = """ [hello] name = alice """ CONFIG_FILE_CONTENTS2 = """ [hello] name = bob """ write_configfile_with_contents("hello.ini", CONFIG_FILE_CONTENTS1) write_configfile_with_contents("hello.cfg", CONFIG_FILE_CONTENTS2) assert os.path.exists("hello.ini") assert os.path.exists("hello.cfg") CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config()) @click.command(context_settings=CONTEXT_SETTINGS) @click.option("-n", "--name", default="__CMDLINE__") def hello(name): click.echo("Hello %s" % name) result = cli_runner_isolated.invoke(hello) assert result.output == "Hello alice\n" assert result.exit_code == 0
def name_to_command(parent, name): try: if sys.version_info[0] == 2: if parent: parent = parent.encode('ascii', 'replace') name = name.encode('ascii', 'replace') if parent: mod_name = 'tripaille.commands.%s.%s' % (parent, name) else: mod_name = 'tripaille.commands.cmd_' + name mod = __import__(mod_name, None, None, ['cli']) except ImportError as e: error("Problem loading command %s, exception %s" % (name, e)) return return mod.cli
def cli_kick(template_name, extra_vars, limit): """ Start an ansible tower job from the command line """ try: # verify configuration config = Config(config_file()) guard = Guard(config) template_id = guard.get_template_id(template_name) extra_v = {} for extra_var in extra_vars: extra_v.update(extra_var_to_dict(extra_var)) job = guard.kick(template_id=template_id, limit=limit, extra_vars=extra_v) job_url = guard.launch_data_to_url(job) print('Started job: {0}'.format(job_url)) except CLIError as error: print(error) sys.exit(1) except GuardError as error: msg = 'Error kicking job tempate: {0} - {1}'.format(template_name, error) print(msg) sys.exit(1)