我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用argcomplete.autocomplete()。
def _setup_command_line_interface(): parser = argparse.ArgumentParser(description=("Setup and manage an " "embedded development " "infrastructure.")) parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity to INFO") parser.add_argument('--log', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help="modify log level (default is WARNING)") subparsers = parser.add_subparsers(title='commands', dest="command_name") for _, command in get_sub_commands().items(): command.advertise(subparsers) argcomplete.autocomplete(parser) return parser
def finalizeOptions(self, availableTargets: list): targetOption = self._parser.add_argument("targets", metavar="TARGET", nargs=argparse.ZERO_OR_MORE, help="The targets to build", choices=availableTargets + [[]]) if argcomplete and "_ARGCOMPLETE" in os.environ: # if IS_FREEBSD: # FIXME: for some reason this won't work excludes = ["-t", "--skip-dependencies"] if sys.platform.startswith("freebsd"): excludes += ["--freebsd-builder-copy-only", "--freebsd-builder-hostname", "--freebsd-builder-output-path"] visibleTargets = availableTargets.copy() visibleTargets.remove("__run_everything__") targetCompleter = argcomplete.completers.ChoicesCompleter(visibleTargets) targetOption.completer = targetCompleter # make sure we get target completion for the unparsed args too by adding another zero_or more options # not sure why this works but it's a nice hack unparsed = self._parser.add_argument("targets", metavar="TARGET", type=list, nargs=argparse.ZERO_OR_MORE, help=argparse.SUPPRESS, choices=availableTargets) unparsed.completer = targetCompleter argcomplete.autocomplete( self._parser, always_complete_options=None, # don't print -/-- by default exclude=excludes, # hide these options from the output print_suppressed=True, # also include target-specific options )
def start(self, run_args=None): """ Starts a command and registers single handlers. Args: run_args (:class:`list`): the list of run arguments. If None we use sys.argv[1:]. """ if run_args is None: run_args = sys.argv[1:] parser = self._get_arg_parser() argcomplete.autocomplete(parser) if self.parse_unknown_args: args, unknown = parser.parse_known_args(run_args) self.run(args, unknown) else: args = parser.parse_args(run_args) self.run(args, {})
def parse_args(): parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter, description=\ "Usage: python brutespray.py <OPTIONS> \n") menu_group = parser.add_argument_group(colors.lightblue + 'Menu Options' + colors.normal) menu_group.add_argument('-f', '--file', help="GNMAP or XML file to parse", required=True) menu_group.add_argument('-o', '--output', help="Directory containing successful attempts", default="brutespray-output") menu_group.add_argument('-s', '--service', help="specify service to attack", default="all") menu_group.add_argument('-t', '--threads', help="number of medusa threads", default="2") menu_group.add_argument('-T', '--hosts', help="number of hosts to test concurrently", default="1") menu_group.add_argument('-U', '--userlist', help="reference a custom username file", default=None) menu_group.add_argument('-P', '--passlist', help="reference a custom password file", default=None) menu_group.add_argument('-u', '--username', help="specify a single username", default=None) menu_group.add_argument('-p', '--password', help="specify a single password", default=None) menu_group.add_argument('-c', '--continuous', help="keep brute-forcing after success", default=False, action='store_true') menu_group.add_argument('-i', '--interactive', help="interactive mode", default=False, action='store_true') argcomplete.autocomplete(parser) args = parser.parse_args() return args
def create_parser(self, prog_name, subcommand): # noqa """ Create and return the ``ArgumentParser`` which will be used to parse the arguments to this command. """ parser = CommandParser( self, prog="%s %s" % (os.path.basename(prog_name), subcommand), description=self.help or None) parser.add_argument( '--traceback', action='store_true', help=SUPPRESS) if self.args: # Keep compatibility and always accept positional arguments, # like optparse when args is set parser.add_argument('args', nargs='*') self.add_arguments(parser) argcomplete.autocomplete(parser) return parser
def run_with_sysargs(): import argparse parser = argparse.ArgumentParser(description='Generates a project.') parser.add_argument('sourcename', type=str, help='name of source recipe/template').completer = TemplateCompleter() parser.add_argument('destname', type=str, help='name of new project to create') parser.add_argument('servicename', type=str, help='optional, name of service to create', nargs='?') if argcomplete: argcomplete.autocomplete(parser) args = parser.parse_args() if (args.servicename): generate(args.sourcename, args.destname, args.servicename) else: generate(args.sourcename, args.destname)
def __call__(self, args=None): if args is None: args = sys.argv[1:] if argcomplete: argcomplete.autocomplete(self.parser, exclude=['-P']) elif 'COMP_LINE' in os.environ: return 0 sys.excepthook = self.handle args = self.read_configuration(args) args = self.parser.parse_args(args) self.configure_logging(args) if args.debug: try: import pudb except ImportError: pudb = None import pdb return (pudb or pdb).runcall(self.main, args) else: return self.main(args) or 0
def __init__(self, application): self.application = application self.parser = parser = argparse.ArgumentParser(description='%s command line interface.' % application.name) parser.add_argument('-p', '--process-name', metavar="PREFIX", default=application.name, help='A string indicates the logger prefix for this process, it helps to configure ' 'separate log files per process.') parser.add_argument('-c', '--config-file', metavar="FILE", help='List of configuration files separated by space. Default: ""') parser.add_argument('-d', '--config-dir', metavar="DIR", help='List of configuration directories separated by space. Default: ""') subparsers = parser.add_subparsers(title="sub commands", prog=application.name, dest="command") AdminLauncher.register(subparsers) ServeLauncher.register(subparsers) MigrateLauncher.register(subparsers) WorkerLauncher.register(subparsers) DevLauncher.register(subparsers) application.register_cli_launchers(subparsers) argcomplete.autocomplete(parser)
def main(): parser = get_parser() if sys.stdin.isatty(): try: import argcomplete except ImportError: pass else: argcomplete.autocomplete(parser) args = parser.parse_args() args = vars(args) args.pop("command") args.pop("subcommand", None) func_name = args.pop("func") func = import_func(func_name) try: result, exit_code = func(**args) except: if not sys.stdin.isatty(): import traceback msg = traceback.format_exc() handle_error(msg) raise else: if result: # nonzero exit codes for non-interactive commands should be # logged if exit_code > 0 and not sys.stdin.isatty() and not sys.stdout.isatty(): handle_error(result) # otherwise print else: print(result) return exit_code
def parse_args(): parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter, description= \ "Usage: python passmaker.py <OPTIONS> \n") menu_group = parser.add_argument_group('Menu Options') menu_group.add_argument('-o', '--output', help="password dict file", default=None) menu_group.add_argument('-i', '--interactive', help="interactive mode",action='store_true',default=False) menu_group.add_argument('-g', '--gui', help="GUI mode", action='store_true', default=False) argcomplete.autocomplete(parser) args = parser.parse_args() return args
def finalizeOptions(self, availableTargets: list, **kwargs): targetOption = self._parser.add_argument("targets", metavar="TARGET", nargs=1, help="The target to build", choices=availableTargets + [EXTRACT_SDK_TARGET]) if "_ARGCOMPLETE" in os.environ: try: import argcomplete except ImportError: sys.exit("argcomplete missing") targetCompleter = argcomplete.completers.ChoicesCompleter(availableTargets) targetOption.completer = targetCompleter argcomplete.autocomplete( self._parser, always_complete_options=None, # don't print -/-- by default print_suppressed=True, # also include target-specific options )
def parseArguments(): "Argument parser for standalone hdlcc" parser = argparse.ArgumentParser() # Options parser.add_argument('--host', action='store',) parser.add_argument('--port', action='store',) parser.add_argument('--attach-to-pid', action='store', type=int) parser.add_argument('--log-level', action='store', ) parser.add_argument('--log-stream', action='store', ) parser.add_argument('--nocolor', action='store_true', default=False) parser.add_argument('--stdout', action='store') parser.add_argument('--stderr', action='store') try: import argcomplete argcomplete.autocomplete(parser) except ImportError: # pragma: no cover pass args = parser.parse_args() args.host = args.host or 'localhost' args.port = args.port or 50000 args.log_level = args.log_level or logging.INFO args.log_stream = args.log_stream or sys.stdout args.color = False if args.nocolor else True del args.nocolor return args # Copied from ycmd!
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('degree', type=int) parser.add_argument('prec', type=int, default=50) parser.add_argument('data', help="""Data of matrix to compute exp of. Should be in scipy sparse csr format.""") parser.add_argument('time', type=float) parser.add_argument('--expr', type=lambda e: sympify(e, locals=globals()), help="""Precomputed CRAM expression. Should have the same prec as 'prec'. If not provided, will be computed from scratch.""") parser.add_argument('--log-level', default=None, choices=['debug', 'info', 'warning', 'error', 'critical']) # TODO: Add options for arguments to pass to various functions as needed. try: import argcomplete argcomplete.autocomplete(parser) except ImportError: pass args = parser.parse_args() if args.log_level: logger.setLevel(getattr(logging, args.log_level.upper())) res = run_transmute_test(args.data, args.degree, args.prec, args.time,expr=args.expr, _print=True) print("Column sums (min, max):") errors = {} colsums = {} for r in sorted(res): if res[r] is None: print('Could not compute', r) continue colsums[r] = np.sum(res[r], axis=1) errors[r] = np.max(colsums[r]) - np.min(colsums[r]) for r in sorted(errors, key=lambda i:errors[i], reverse=True): print(r, np.min(colsums[r]), np.max(colsums[r]))
def main(): p = make_parser() try: import argcomplete argcomplete.autocomplete(p) except ImportError: pass args = p.parse_args() execute(**vars(args))
def _get_arg_parser(self, doc_parser=False): """Create the auto parser. This should be implemented by the implementing class. To enable autocomplete in your shell please execute activate-global-python-argcomplete in your shell. The arg parser should support two output modes, one for the command line (default) and one for generating the documentation. If the flag doc_parser is true the parser should be generated for the documentation. Args: doc_parser (boolean): If true the parser should be prepared for the documentation. If false (the default) the parser should be generated for the command line. Returns: argparse.ArgumentParser: the argument parser """ description = textwrap.dedent(""" Basic parser introduction here. Can be multiline. """) epilog = textwrap.dedent(""" Examples of use: mdt-model-fit "BallStick (Cascade)" data.nii.gz data.prtcl roi_mask_0_50.nii.gz """) parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawTextHelpFormatter) return parser
def main(): parser = argparse.ArgumentParser(description='Upload or download files/directories to or from an S3 bucket') subparsers = parser.add_subparsers(dest='mode') upload_file_parser = subparsers.add_parser('up_file', help='Upload individual file to S3 bucket') upload_file_parser.add_argument('-f', '--file_loc', required=True, action='store', dest='file_loc', help='Full path to filename') upload_file_parser.add_argument('-b', '--bucket', required=True, action='store', dest='bucket_name', help='Name of the S3 bucket') upload_file_parser.add_argument('-fs', '--file_store', required=True, action='store', dest='file_store', help='How the file will be named in S3 bucket') upload_dir_parser = subparsers.add_parser('up_dir', help='Upload directory to S3 bucket') upload_dir_parser.add_argument('-d', '--dir_loc', required=True, action='store', dest='dir_loc', help='Full path to directory') upload_dir_parser.add_argument('-b', '--bucket', required=True, action='store', dest='bucket_name', help='Name of the S3 bucket') dnload_file_parser = subparsers.add_parser('dn_file', help='Download individual file from S3 bucket') dnload_file_parser.add_argument('-b', '--bucket', required=True, action='store', dest='bucket_name', help='Name of the S3 bucket') dnload_file_parser.add_argument('-f', '--file_name', required=True, action='store', dest='file_name', help='Name of file in S3 bucket') argcomplete.autocomplete(parser) args = parser.parse_args() s3 = boto3.resource('s3') if args.mode == 'up_file': print('Uploading: ' + args.file_loc) s3.Bucket(args.bucket_name).put_object(Key=args.file_store, Body=open(args.file_loc)) if args.mode == 'up_dir': for path, dirs, files in os.walk(args.dir_loc): for filename in files: print('Uploading: ' + (path + filename)) s3.Bucket(args.bucket_name).put_object(Key=filename, Body=open(path + filename, 'rb')) if args.mode == 'dn_file': s3.meta.client.download_file(args.bucket_name, args.file_name, './' + args.file_name)
def autocomplete(parser): """ Adds support for shell completion via argcomplete_ by patching given `argparse.ArgumentParser` (sub)class. If completion is not enabled, logs a debug-level message. """ if COMPLETION_ENABLED: argcomplete.autocomplete(parser) elif 'bash' in os.getenv('SHELL', ''): logger.debug('Bash completion not available. Install argcomplete.')
def try_argcomplete(parser): try: import argcomplete except ImportError: pass else: argcomplete.autocomplete(parser)
def try_argcomplete(parser): if os.environ.get('_ARGCOMPLETE'): try: import argcomplete except ImportError: pass else: argcomplete.autocomplete(parser)
def main(): parser = setup_parser() argcomplete.autocomplete(parser) options = parser.parse_args() _setup_logger(options) # Support the deprecated -c option if getattr(options, 'config', None) is not None: options.configs.append(options.config) if options.subparser in ('report', 'logs', 'metrics', 'run'): _default_region(options) _default_account_id(options) try: command = options.command if not callable(command): command = getattr( importlib.import_module(command.rsplit('.', 1)[0]), command.rsplit('.', 1)[-1]) # Set the process name to something cleaner process_name = [os.path.basename(sys.argv[0])] process_name.extend(sys.argv[1:]) setproctitle(' '.join(process_name)) command(options) except Exception: if not options.debug: raise traceback.print_exc() pdb.post_mortem(sys.exc_info()[-1])
def enable_autocomplete(parser): argcomplete.autocomplete = argcomplete.CompletionFinder() argcomplete.autocomplete(parser, validator=lambda c, p: c.lower().startswith(p.lower()), default_completer=lambda _: ())
def parseArg(): """ This fonction will the list of pdb files and the distance @return: dictionnary of arguments Ex : python Cluster_Analysis.py -f *.pdb -s A:1-30:CA """ arguments=argparse.ArgumentParser(description="This program was developped in order to clusterize " "molecular dynamictrajectories (Amber, gromacs, chamm, namd, PDB)") try: argcomplete.autocomplete(arguments) except: pass arguments.add_argument('-f', "--traj", help="trajectory file", required=True) arguments.add_argument('-t','--top', help="topfile", default=None) arguments.add_argument('-l','--logfile', help="logfile (default : clustering.log). The " "name of your output file will be the basename (name before the extention " "of this logfile", default="clustering") arguments.add_argument('-st','--select_traj', help="selection syntaxe for " "Don't forget to add QUOTES besite this selection string." "trajectory extraction (default : all).", default="all") arguments.add_argument('-sa','--select_alignement', help="selection syntaxe" " for alignement (default : backbone). Don't forget to add QUOTES besite this " "selection string." " If you don't want aligment use \"none\".", default="backbone") arguments.add_argument('-sr','--select_rmsd', help="selection syntaxe for " " RMSD (default : backbone). Don't forget to add QUOTES " "besite this selection string.", default="backbone") #Clustering arguments arguments.add_argument('-m','--method', help="method for clustering : single " "; complete; average; weighted; centroid; median. (ward)", default="ward") arguments.add_argument('-cc',"--cutoff", help="cutoff for clusterization from " "hierarchical clusturing with Scipy", default=None) arguments.add_argument('-ng',"--ngroup", help="number of group asked. Use the " "maxclust method to clusterize in this case", default=None) #Interactive mode for distance matrix: arguments.add_argument('-i','--interactive', help="Interactive mode for distance matrix (Y/n)", default="Y") args = vars(arguments.parse_args()) return(args)
def init(): import argcomplete if get_commands() is not None: raise RuntimeError("Commands are already initialised") init_commands() # autocompletion argcomplete.autocomplete(get_default_parser()) return get_commands()
def __call__ (self): self.prep_parser( ) self.configure_parser(self.parser) argcomplete.autocomplete(self.parser, always_complete_options=self.always_complete_options); # print "FIXED INPUTS??", self.inputs self.args = self.parser.parse_args(self.inputs) self.prolog( ) self.run(self.args) self.epilog( )
def main(args=None): CoreServices.setup() # inject existing libraries. # because of that all the ansible modules should be imported after that CoreServices.dependency_manager().inject_libraries() specs_manager = api.SpecManager() # Init Managers specs_manager.register_spec( WorkspaceManagerSpec('workspace', description="Workspace manager. " "Allows to create and use an " "isolated environment for plugins " "execution.")) specs_manager.register_spec( PluginManagerSpec('plugin', description="Plugin management")) specs_manager.register_spec( SSHSpec( 'ssh', description="Interactive ssh session to node from inventory.")) # register all plugins for plugin in CoreServices.plugins_manager().PLUGINS_DICT.values(): specs_manager.register_spec(api.InfraredPluginsSpec(plugin)) argcomplete.autocomplete(specs_manager.parser) return specs_manager.run_specs(args) or 0
def enable_autocomplete(self, parser): if self.cli_ctx.data['completer_active']: argcomplete.autocomplete = argcomplete.CompletionFinder() argcomplete.autocomplete(parser, validator=lambda c, p: c.lower().startswith(p.lower()), default_completer=lambda _: ())
def cmany_main(in_args=None): if in_args is None: in_args = sys.argv[1:] in_args = c4args.merge_envargs(cmds, in_args) mymod = sys.modules[__name__] parser = c4args.setup(cmds, mymod) argcomplete.autocomplete(parser) args = c4args.parse(parser, in_args) if args: args.func(args) # -----------------------------------------------------------------------------
def __init__(self): self.parser = parser = argparse.ArgumentParser(description='Restfulpy command line interface.') subparsers = parser.add_subparsers(title="Restfulpy sub commands", prog=basename(sys.argv[0]), dest="command") from restfulpy.mockupservers import SimpleMockupServerLauncher SimpleMockupServerLauncher.register(subparsers) argcomplete.autocomplete(parser)
def get_opt_parser( ): parser = argparse.ArgumentParser( ) parser.add_argument('infile', nargs="+", default=sys.stdin, type=argparse.FileType('r'), help="Find dates in this file.") parser.add_argument('--collate', dest='collate', default=False, action='store_true') parser.add_argument('--data', choices=['glucose', 'pump'], default='pump') parser.add_argument('--model', # type=get_model, choices=models.known.keys( )) parser.add_argument('--larger', dest='larger', action='store_true') parser.add_argument('--no-larger', dest='larger', action='store_false') parser.add_argument('--out', default=sys.stdout, type=argparse.FileType('w'), help="Write records here.") parser.set_defaults(larger=False) argcomplete.autocomplete(parser) return parser ## # move to history.py #
def __init__(self): self.env = self.parse_env( ) self.parser = self.get_parser( ) self.autocomplete( )
def autocomplete (self): try: import argcomplete argcomplete.autocomplete(self.parser) except ImportError: # no auto completion pass
def create_parser(self, prog, func_stack=(), parent=None): """ Creates an ArgumentParser instance from options returned by get_options(), and subparser for the given commands. """ prog = os.path.basename(prog) func_stack=func_stack+(self,) options_parser = argparse.ArgumentParser(add_help=False) for option in self.get_options(): options_parser.add_argument(*option.args, **option.kwargs) parser = argparse.ArgumentParser(prog=prog, usage=self.usage, description=self.description, parents=[options_parser], add_help=False) add_help(parser, self.help_args) self._patch_argparser(parser) subparsers = parser.add_subparsers() for name, command in self._commands.items(): usage = getattr(command, 'usage', None) help = getattr(command, 'help', None) if help is None: help = command.__doc__ description = getattr(command, 'description', None) if description is None: description = command.__doc__ command_parser = command.create_parser(name, func_stack=func_stack, parent=self) subparser = subparsers.add_parser(name, usage=usage, help=help, description=description, parents=[command_parser], add_help=False) if isinstance(command, Manager): self._patch_argparser(subparser) ## enable autocomplete only for parent parser when argcomplete is ## imported and it is NOT disabled in constructor if parent is None and ARGCOMPLETE_IMPORTED \ and not self.disable_argcomplete: argcomplete.autocomplete(parser, always_complete_options=True) self.parser = parser return parser # def foo(self, app, *args, **kwargs): # print(args)
def main(): parser = argparse.ArgumentParser() parser.add_argument('-v', '--verbose', action='count', default=0, dest='verbosity') parser.add_argument('-V', '--version', action='store_true', dest='version', help='Show version and exit') input_group = parser.add_mutually_exclusive_group() input_group.add_argument('file', nargs='?', metavar='FILE', help='The path to the markdown file') input_group.add_argument('--stdin', dest='stdin', action='store_true', help='Read Markdown from stdin') parser.add_argument('-t', '--tab-spaces', dest='tab_spaces', default=4, type=int, help='Number of spaces in a tab (defaults to 4)') try: import argcomplete argcomplete.autocomplete(parser) except Exception: pass # Optional argcomplete module not installed args = parser.parse_args() if args.version: print('vmd {}'.format(VERSION)) sys.exit(0) import logging if args.verbosity != 0: logging_level = 10 * max(0, 3 - args.verbosity) logging.basicConfig(level=logging_level) mdparser = build_parser(args) config = load_config() writer = create_display_writer(sys.stdout) renderer = build_render(writer, config) if args.stdin: doc = mdparser.parse(sys.stdin.read()) elif args.file is not None: with open(args.file) as f: doc = mdparser.parse(f.read()) else: parser.print_help() sys.exit(1) renderer.render_document(doc) sys.stdout.write('\n') sys.stdout.flush() sys.stdout.close()
def parseArguments(): "Argument parser for standalone hdlcc" if ('--version' in sys.argv[1:]) or ('-V' in sys.argv[1:]): # pragma: no cover print(hdlcc.__version__) sys.exit(0) parser = argparse.ArgumentParser() # Options parser.add_argument('--version', action='store_true', help="Shows hdlcc version and exit") parser.add_argument('--verbose', '-v', action='append_const', const=1, help="""Increases verbose level. Use multiple times to increase more""") parser.add_argument('--clean', '-c', action='store_true', help="Cleans the project before building") parser.add_argument('--sources', '-s', action='append', nargs='*', default=[], help="""Source(s) file(s) to build individually""") \ .completer = _fileExtentensionCompleter('vhd') parser.add_argument('--debug-print-sources', action='store_true') parser.add_argument('--debug-parse-source-file', action='store_true') parser.add_argument('--debug-run-static-check', action='store_true') parser.add_argument('--debug-profiling', action='store', nargs='?', metavar='OUTPUT_FILENAME', const='hdlcc.pstats') # Mandatory arguments parser.add_argument('project_file', action='store', nargs=1, help="""Configuration file that defines what should be built (lists sources, libraries, build flags and so on""") if _HAS_ARGCOMPLETE: # pragma: no cover argcomplete.autocomplete(parser) args = parser.parse_args() # PYTHON_ARGCOMPLETE_OK args.project_file = args.project_file[0] args.log_level = logging.FATAL if args.verbose: if len(args.verbose) == 1: args.log_level = logging.WARNING elif len(args.verbose) == 2: args.log_level = logging.INFO else: args.log_level = logging.DEBUG # Planify source list if supplied if args.sources: args.sources = [source for sublist in args.sources for source in sublist] return args
def create_parser(self, prog, parents=None): """ Creates an ArgumentParser instance from options returned by get_options(), and a subparser for the given command. """ prog = os.path.basename(prog) options_parser = argparse.ArgumentParser(add_help=False) for option in self.get_options(): options_parser.add_argument(*option.args, **option.kwargs) # parser_parents = parents if parents else [option_parser] # parser_parents = [options_parser] parser = argparse.ArgumentParser(prog=prog, usage=self.usage, description=self.description, parents=[options_parser]) subparsers = parser.add_subparsers() for name, command in self._commands.items(): usage = getattr(command, 'usage', None) help = getattr(command, 'help', command.__doc__) description = getattr(command, 'description', command.__doc__) # Only pass `parents` argument for commands that support it try: command_parser = command.create_parser(name, parents=[options_parser]) except TypeError: warnings.warn("create_parser for {0} command should accept a `parents` argument".format(name), DeprecationWarning) command_parser = command.create_parser(name) subparser = subparsers.add_parser(name, usage=usage, help=help, description=description, parents=[command_parser], add_help=False) ## enable autocomplete only for parent parser when argcomplete is ## imported and it is NOT disabled in constructor if parents is None and ARGCOMPLETE_IMPORTED \ and not self.disable_argcomplete: argcomplete.autocomplete(parser, always_complete_options=True) return parser # def foo(self, app, *args, **kwargs): # print(args)
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('degree') parser.add_argument('prec', type=int) parser.add_argument('--division', type=int) parser.add_argument('--c', type=float) parser.add_argument('--maxsteps', type=int) parser.add_argument('--max-loops', type=int) parser.add_argument('--convergence-value', type=mpmath.mpf) parser.add_argument('--tol', type=mpmath.mpf) parser.add_argument('--nsolve-type', default=None, choices=['points', 'intervals']) parser.add_argument('--solver', default=None) parser.add_argument('--D-scale', default=None, type=float) parser.add_argument('--scale', default=None, type=bool) parser.add_argument('--plot', default=True, type=bool) parser.add_argument('--seed', default=None, type=int) parser.add_argument('--initial-points', default=None, choices=['chebyshev', 'random']) parser.add_argument('--log-to-file', default=True, type=bool, help="Log output to a file (in the logs/ directory)") parser.add_argument('--log-level', default='info', choices=['debug', 'info', 'warning', 'error', 'critical']) parser.add_argument('--save-cache', action="store_true", default=False, help="""Save the computed CRAM expression to the local cache.""") try: import argcomplete argcomplete.autocomplete(parser) except ImportError: pass args = parser.parse_args() try: args.degree = ast.literal_eval(args.degree) except (ValueError, SyntaxError) as e: parser.error("Invalid degree: " + str(e)) arguments = args.__dict__.copy() for i in arguments.copy(): if not arguments[i]: del arguments[i] if args.log_level: logger.setLevel(getattr(logging, args.log_level.upper())) del arguments['log_level'] expr = CRAM_exp(**arguments) if args.save_cache: local_cache_file = get_local_CRAM_cache_file(args.degree, args.prec) print("Saving to", local_cache_file) with open(local_cache_file, 'w') as f: f.write(srepr(expr))
def main(): parser = argparse.ArgumentParser(description='Create a corpus from a collection of tweets and/or build an LDA model') parser.add_argument('-t', '--topology_file', required=True, action='store', dest='top_file', help='Location of topology file') parser.add_argument('-p', '--dir_prefix', choices=['clique', 'community'], required=True, action='store', dest='dir_prefix', help='Select whether the topology contains cliques or communities') parser.add_argument('-w', '--working_dir', required=True, action='store', dest='working_dir', help='Name of the directory you want to direct output to') parser.add_argument('-l', '--lda_loc', required=True, action='store', dest='lda_loc', help='Location of the saved LDA model') parser.add_argument('-d', '--dict_loc', required=True, action='store', dest='dict_loc', help='Location of dictionary for the model') parser.add_argument('-u', '--unseen_docs', required=True, action='store', dest='unseen_docs', help='Directory containing unseen documents') parser.add_argument('-m', '--lemma', action='store_true', dest='lemma', help='Use this option to lemmatize words') argcomplete.autocomplete(parser) args = parser.parse_args() output_dir = args.working_dir + '/' if not os.path.exists(os.path.dirname(output_dir)): os.makedirs(os.path.dirname(output_dir), 0o755) # load dictionary model_dict = corpora.Dictionary.load(args.dict_loc) # load trained model from file lda = models.LdaModel.load(args.lda_loc) write_topn_words(output_dir, lda) with open(args.top_file, 'r') as inp_file: users = set(str(user) for community in inp_file for user in ast.literal_eval(community)) try: with open(output_dir + 'document_vectors.json', 'r') as all_community_file: document_vectors = json.load(all_community_file) except: document_vectors = {} pool = multiprocessing.Pool(max(1, multiprocessing.cpu_count() - 1)) func = partial(get_document_vectors, tweets_dir=args.unseen_docs, document_vectors=document_vectors, dictionary=model_dict, lda_model=lda, lemma=args.lemma) doc_vecs = pool.map(func, users) doc_vecs = [item for item in doc_vecs if item is not None] pool.close() pool.join() doc_vecs = dict(doc_vecs) document_vectors.update(doc_vecs) with open(output_dir + 'document_vectors.json', 'w') as document_vectors_file: json.dump(document_vectors, document_vectors_file, sort_keys=True, indent=4) print('Building directories') with open(args.top_file, 'r') as topology_file: for i, community in enumerate(topology_file): community_dir = output_dir + args.dir_prefix + '_' + str(i) + '/' if not os.path.exists(os.path.dirname(community_dir)): os.makedirs(os.path.dirname(community_dir), 0o755) comm_doc_vecs = community_document_vectors(doc_vecs, community) with open(community_dir + 'community_doc_vecs.json', 'w') as comm_docs_file: json.dump(comm_doc_vecs, comm_docs_file, sort_keys=True, indent=4)
def main(): parser = argparse.ArgumentParser(description='Create a corpus from a collection of tweets and/or build an LDA model') subparsers = parser.add_subparsers(dest='mode') text_corpus_parser = subparsers.add_parser('text', help='Build corpus from directory of text files') text_corpus_parser.add_argument('-d', '--docs_loc', required=True, action='store', dest='docs_loc', help='Directory where tweet documents stored') text_corpus_parser.add_argument('-c', '--corp_loc', required=True, action='store', dest='corp_loc', help='Location and name to save corpus') text_corpus_parser.add_argument('-m', '--lemma', action='store_true', dest='lemma', help='Use this option to lemmatize words') wiki_corpus_parser = subparsers.add_parser('wiki', help='Build corpus from compressed Wikipedia articles') wiki_corpus_parser.add_argument('-w', '--wiki_loc', required=True, action='store', dest='wiki_loc', help='Location of compressed Wikipedia dump') wiki_corpus_parser.add_argument('-c', '--corp_loc', required=True, action='store', dest='corp_loc', help='Location and name to save corpus') wiki_corpus_parser.add_argument('-m', '--lemma', action='store_true', dest='lemma', help='Use this option to lemmatize words') lda_model_parser = subparsers.add_parser('lda', help='Create LDA model from saved corpus') lda_model_parser.add_argument('-c', '--corp_loc', required=True, action='store', dest='corp_loc', help='Location of corpus') lda_model_parser.add_argument('-d', '--dict_loc', required=True, action='store', dest='dict_loc', help='Location of dictionary') lda_model_parser.add_argument('-n', '--num_topics', required=True, action='store', dest='num_topics', help='Number of topics to assign to LDA model') lda_model_parser.add_argument('-p', '--num_pass', required=True, action='store', dest='num_pass', help='Number of passes through corpus when training the LDA model') lda_model_parser.add_argument('-l', '--lda_loc', required=True, action='store', dest='lda_loc', help='Location and name to save LDA model') lda_vis_parser = subparsers.add_parser('ldavis', help='Create visualization of LDA model') lda_vis_parser.add_argument('-c', '--corp_loc', required=True, action='store', dest='corp_loc', help='Location of corpus') lda_vis_parser.add_argument('-d', '--dict_loc', required=True, action='store', dest='dict_loc', help='Location of dictionary') lda_vis_parser.add_argument('-l', '--lda_loc', required=True, action='store', dest='lda_loc', help='Location of LDA model') argcomplete.autocomplete(parser) args = parser.parse_args() if args.mode == 'text': doc_corpus = DocCorpus(args.docs_loc, args.lemma) doc_corpus.dictionary.filter_extremes(no_below=1, no_above=0.5, keep_n=DEFAULT_DICT_SIZE) MmCorpus.serialize(args.corp_loc + '.mm', doc_corpus) doc_corpus.dictionary.save(args.corp_loc + '.dict') if args.mode == 'wiki': if args.lemma: wiki_corpus = WikiCorpus(args.wiki_loc, lemmatize=True, tokenizer_func=wiki_tokenizer, article_min_tokens=100, token_min_len=3, token_max_len=15) else: wiki_corpus = WikiCorpus(args.wiki_loc, lemmatize=False, tokenizer_func=wiki_tokenizer, article_min_tokens=100, token_min_len=3, token_max_len=15) wiki_corpus.dictionary.filter_extremes(no_below=5, no_above=0.5, keep_n=DEFAULT_DICT_SIZE) MmCorpus.serialize(args.corp_loc + '.mm', wiki_corpus) wiki_corpus.dictionary.save(args.corp_loc + '.dict') if args.mode == 'lda': build_LDA_model(args.corp_loc, args.dict_loc, args.num_topics, args.num_pass, args.lda_loc) if args.mode == 'ldavis': build_pyLDAvis_output(args.corp_loc, args.dict_loc, args.lda_loc)
def main(): hc.setup() opt = "stdin_buffer_lines" buffer = 0 buffering = False #if opt in CONFIG: # buffering = True # buffer = int(CONFIG[opt]) if not sys.stdin.isatty(): db = get_database() cbuffer = 0 # FIXME: should handle g-code & stuff while True: line = sys.stdin.readline() if not line: break path, value = line.split(' ', 1) if ' ' in value: # timestamp present dt_in, value = value.split() dt = parse_timestamp(dt_in) else: dt = now() #print(o) cbuffer += 1 sys.exit(0) #logging.basicConfig(level=logging.DEBUG) #logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) logging.basicConfig(level=logging.INFO) parser = ArghParser() parser.add_commands([ send, shell, server, monitor, uimon, uipcb, uiprobe, ]) argcomplete.autocomplete(parser) try: parser.dispatch() except KeyboardInterrupt: sys.exit(1)
def __init__(self, name, description, commandline_args=[]): """Command line arguments can be a list of shortcuts from `predefined_args`, or a list of dictionaries. Arguments can also be put in a file named SCRIPTNAME_args.py, e.g. `harvest_args.py`. """ self.parser = argparse.ArgumentParser(description) # Add one ubiqitous command line arguments commandline_args += ["loglevel"] # Check for FILENAME_args.py file import __main__ import os try: filename = os.path.basename(__main__.__file__) filename = os.path.splitext(filename)[0] args_from_file = __import__(filename + "_args") commandline_args += args_from_file.args except ImportError: pass # Add all the command line arguments for c in commandline_args: # cCheck for shortcuts used if isinstance(c, str): c = self.predefined_args[c] self.parser.add_argument( c.pop("short", None), c.pop("long", None), **c) argcomplete.autocomplete(self.parser) self.args = self.parser.parse_args() self.logger = logging.getLogger(name) # https://docs.python.org/2/library/logging.html#levels self.logger.setLevel(self.args.loglevel * 10) self.executionMode = self.NORMAL_MODE # Convenience shortcuts to logger methods
def _parse_args(self): """Setup parser and parse cli arguments. NB! counter-intuitively, this function also messes around with logging levels. """ # We want some of our options to take effect as early as possible, as they affect command line parsing. # For these options we resort to some ugly, basic argv spotting if '--debug' in sys.argv: logging.getLogger().setLevel(logging.DEBUG) logging.debug('Early debug enabled') if '--verbose' in sys.argv or '-v' in sys.argv: logging.getLogger().setLevel(logging.INFO) autodiscover = False if '--autodiscover' in sys.argv: logging.debug('Autodiscover enabled') autodiscover = True parser = SmartCommandMapParser(prog=self.tool_shortname, description="Cligraphy command line tools", formatter_class=CustomDescriptionFormatter) self.parser = parser # expose to eg. ctx parser.add_argument('--version', action=_VersionAction, nargs=0, dest="_version") parser.add_argument("--debug", help="enable debuging output", dest="_level", action="store_const", const=logging.DEBUG) parser.add_argument("--pdb", help="run pdb on exceptions", dest="_pdb", action="store_true") parser.add_argument("--no-capture", help="disable input/output capture", dest="_capture", action="store_false", default=True) parser.add_argument("--no-reporting", help="disable reporting", dest="_reporting", action="store_false", default=True) parser.add_argument("--profile", help="enable profiling", dest="_profile", action="store_true", default=False) parser.add_argument("--autodiscover", help="re-discover commands and refresh cache (default: read cached commands list)", dest="_autodiscover", action="store_true") parser.add_argument("-v", "--verbose", help="enable informational output", dest="_level", action="store_const", const=logging.INFO) for namespace, command_map in self.get_command_maps(autodiscover): parser.add_command_map(namespace, command_map) argcomplete.autocomplete(parser) _warn_about_bad_non_ascii_chars(sys.argv) _warn_about_bad_path(os.getenv('VIRTUAL_ENV'), os.getenv('PATH')) args = parser.parse_args() args._parser = parser # deprecated # pylint:disable=protected-access if args._level is not None: logging.getLogger().setLevel(args._level) return args