コード例 #1
0
def make_parser():
    """Use argparse to make a parser for configuration options."""
    o = datatypes.ParserWrapper(
        argparse.ArgumentParser(
            usage="%(prog)s [options] input",
            description="Infer/check types in a Python module"))

    # Input files
    o.add_argument("input", nargs="*", help="File to process")

    # Modes
    o.add_argument(
        "-C",
        "--check",
        action="store_true",
        dest="check",
        default=None,
        help=("Don't do type inference. Only check for type errors."))
    o.add_argument("-o",
                   "--output",
                   type=str,
                   action="store",
                   dest="output",
                   default=None,
                   help=("Output file. Use '-' for stdout."))

    # Options
    add_basic_options(o)
    add_feature_flags(o)
    add_subtools(o)
    add_pickle_options(o)
    add_infrastructure_options(o)
    add_debug_options(o)
    return o
コード例 #2
0
ファイル: datatypes_test.py プロジェクト: zenefits/pytype
 def test_group(self):
     parser = argparse.ArgumentParser()
     wrapper = datatypes.ParserWrapper(parser)
     wrapper.add_argument("--foo", dest="foo")
     group = wrapper.add_argument_group("test1")
     group.add_argument("--bar", dest="bar")
     subgroup = wrapper.add_argument_group("test2")
     subgroup.add_argument("--baz", dest="baz")
     self.assertSetEqual(set(wrapper.actions), {"foo", "bar", "baz"})
コード例 #3
0
ファイル: parse_args.py プロジェクト: astroparam/pytype
def make_parser():
  """Make parser for command line args.

  Returns:
    A Parser object.
  """

  parser = argparse.ArgumentParser(usage='%(prog)s [options] input [input ...]')
  parser.register('action', 'flatten', _FlattenAction)
  modes = parser.add_mutually_exclusive_group()
  modes.add_argument(
      '--tree', dest='tree', action='store_true', default=False,
      help='Display import tree.')
  modes.add_argument(
      '--unresolved', dest='unresolved', action='store_true', default=False,
      help='Display unresolved dependencies.')
  modes.add_argument(
      '--generate-config', dest='generate_config', type=str, action='store',
      default='',
      help='Write out a dummy configuration file.')
  parser.add_argument(
      '-v', '--verbosity', dest='verbosity', type=int, action='store',
      default=1,
      help='Set logging level: 0=ERROR, 1=WARNING (default), 2=INFO.')
  parser.add_argument(
      '--config', dest='config', type=str, action='store', default='',
      help='Configuration file.')
  parser.add_argument(
      '--version', action='store_true', dest='version', default=None,
      help=('Display pytype version and exit.'))

  # Adds options from the config file.
  types = config.make_converters()
  # For nargs=*, argparse calls type() on each arg individually, so
  # _FlattenAction flattens the list of sets of paths as we go along.
  for option in [
      (('-x', '--exclude'), {'nargs': '*', 'action': 'flatten'}),
      (('inputs',), {'metavar': 'input', 'nargs': '*', 'action': 'flatten'}),
      (('-k', '--keep-going'), {'action': 'store_true', 'type': None}),
      (('-j', '--jobs'), {'action': 'store', 'type': parse_jobs,
                          'metavar': 'N'}),
      (('-P', '--pythonpath'),),
      (('-V', '--python-version'),)
  ]:
    _add_file_argument(parser, types, *option)
  output = parser.add_mutually_exclusive_group()
  _add_file_argument(output, types, ('-o', '--output'))
  output.add_argument(
      '-n', '--no-cache', dest='no_cache', action='store_true', default=False,
      help='Send pytype output to a temporary directory.')

  # Adds options from pytype-single.
  wrapper = datatypes.ParserWrapper(parser)
  pytype_config.add_basic_options(wrapper)
  pytype_config.add_feature_flags(wrapper)
  return Parser(parser, wrapper.actions)
コード例 #4
0
ファイル: arg_parser.py プロジェクト: astroparam/pytype
def add_pytype_and_parse(parser, argv):
    """Add basic pytype options and parse args.

  Useful to generate a quick CLI for a library.

  Args:
    parser: An argparse.ArgumentParser
    argv: Raw command line args, typically sys.argv[1:]

  Returns:
    A tuple of (
      parsed_args: argparse.Namespace,
      pytype_options: pytype.config.Options)
  """
    # Add default --debug and input arguments.
    parser.add_argument("--debug",
                        action="store_true",
                        dest="debug",
                        default=None,
                        help="Display debug output.")
    parser.add_argument("inputs",
                        metavar="input",
                        nargs=1,
                        help="A .py file to index")

    # Add options from pytype-single.
    wrapper = datatypes.ParserWrapper(parser)
    pytype_config.add_basic_options(wrapper)
    pytype_config.add_feature_flags(wrapper)
    parser = Parser(parser, wrapper.actions)

    # Parse argv
    args = parser.parse_args(argv)
    cli_args = args.inputs.copy()

    # Make sure we have a valid set of CLI options to pytype

    ## If we are passed an imports map we should look for pickled files as well.
    if getattr(args, "imports_info", None):
        cli_args += [
            "--imports_info", args.imports_info, "--use-pickled-files"
        ]

    ## We need to set this when creating Options (b/128032570)
    if args.python_version:
        cli_args += ["-V", pytype_utils.format_version(args.python_version)]

    pytype_options = pytype_config.Options(cli_args, command_line=True)
    pytype_options.tweak(**parser.get_pytype_kwargs(args))
    return (args, pytype_options)
コード例 #5
0
def make_parser():
    """Construct a parser to run tests against."""

    parser = argparse.ArgumentParser()
    parser.add_argument('-v',
                        '--verbosity',
                        dest='verbosity',
                        type=int,
                        action='store',
                        default=1)
    parser.add_argument('--config',
                        dest='config',
                        type=str,
                        action='store',
                        default='')

    # Add options from pytype-single.
    wrapper = datatypes.ParserWrapper(parser)
    pytype_config.add_basic_options(wrapper)
    return arg_parser.Parser(parser, wrapper.actions)
コード例 #6
0
def make_parser():
    """Make parser for command line args.

  Returns:
    A Parser object.
  """
    def add_kythe_field(parser, field):
        parser.add_argument("--" + field,
                            dest=field,
                            type=str,
                            action="store",
                            default="",
                            help="Part of kythe's file-level vname proto.")

    parser = argparse.ArgumentParser(usage="%(prog)s [options] input")
    add_kythe_field(parser, "kythe_corpus")
    add_kythe_field(parser, "kythe_root")
    add_kythe_field(parser, "kythe_path")
    parser.add_argument("inputs",
                        metavar="input",
                        nargs=1,
                        help="A .py file to index")
    parser.add_argument("--debug",
                        action="store_true",
                        dest="debug",
                        default=None,
                        help="Display debug output.")
    # TODO(b/124802213): There should be a cleaner way to do this.
    parser.add_argument("--imports_info",
                        type=str,
                        action="store",
                        dest="imports_info",
                        default=None,
                        help="Information for mapping import .pyi to files. ")
    # Add options from pytype-single.
    wrapper = datatypes.ParserWrapper(parser)
    pytype_config.add_basic_options(wrapper)
    return arg_parser.Parser(parser, wrapper.actions)