“Argparse требует аргументов” Ответ

Argeparse может получить список типов

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
Exuberant Earthworm

Argparse требует аргументов

parser.add_argument('--use-lang', required=True, help="Output language")
Snackety Snack

Python Argparse необязательно требуется

# Short answer:
# With argparse, parameters starting with - or -- are considered optional by
# default.

# Longer answer:
# With argparse, parameters starting with - or -- are considered optional by
# default. All other parameters are positional parameters and are required
# by default. It is possible to require optional arguments, but this is a bit
# against their design. Since they are still part of the non-positional
# arguments, they will still be listed under the confusing header 
# “optional arguments” even if they are required. The missing square brackets
# in the usage part however show that they are indeed required.

# Solution:
# I create three categories as follows:
parser._action_groups.pop() # remove existing groups
required_pos = parser.add_argument_group('Required Positional Arguments')
required_nam = parser.add_argument_group('Required Named Arguments')
optional = parser.add_argument_group('Optional Arguments')

# To add arguments to the relevant category, use syntax like:
required_pos.add_argument('dataset', help='path to dataset')
required_nam.add_argument('-o', '--outfile', required=True, 
                          help='path to output file')
optional.add_argument('-t', '--threads', type=int, default=10,
                      help='number of CPUs to use. [default: %(default)s]')

# This creates help messages like:
Required Positional Arguments:
  dataset               path to dataset

Required Named Arguments:
  -o OUTFILE, --outdir OUTFILE
                        path to output file

Optional Arguments:
  -t THREADS, --threads THREADS
                        number of CPUs to use. [default: 10]
Charles-Alexandre Roy

Argparse принимает лишь несколько вариантов

...
parser.add_argument('--val',
                    choices=['a', 'b', 'c'],
                    help='Special testing value')

args = parser.parse_args(sys.argv[1:])
Anxious Aardvark

Флаги Python Argparser

parser.add_argument("-v", "--verbose", action="store_true",
                    help="verbose output")
Delightful Dormouse

Ответы похожие на “Argparse требует аргументов”

Вопросы похожие на “Argparse требует аргументов”

Смотреть популярные ответы по языку

Смотреть другие языки программирования