Ein Hinweis zu argparse, dem Python-Befehlszeilen-Parsing-Tool
import argparse
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Concatenates a person's files",
epilog='''Notices:
1. You need an account first.
2. Files should exist.
'''
)
parser.add_argument(
'-o', '--outfile', default='foo.txt', help='Output file name')
parser.add_argument(
'-q', '--quiet', action='store_true', dest='qFlag',
help='No messages to console')
parser.add_argument(
'-r', '--repeat', type=int, default=1, help='Repeat count')
parser.add_argument(
'-m', '--mode', required=True, choices=['t', 'b'], help='Mode')
parser.add_argument('person', help='Person name')
# mandatory positional argument
parser.add_argument('infile', nargs='*', help='Input file name')
# '+' can also be used for nargs
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--private', action='store_true')
group.add_argument('--public', action='store_true')
args = parser.parse_args()
print(f'outfile = {args.outfile}') # long name is prefered
print(f'quiet = {args.qFlag}') # dest='qFlag'
print(f'repeat count = {args.repeat * 100}') # type=int
print(f'# of infiles = {len(args.infile)}') # stored as a list
main()
Ausführungsbeispiel
$ ./a.py -m t --private uid a b c
outfile = foo.txt
quiet = False
repeat count = 100
# of infiles = 3
args. Name
.action = 'store_true'
type = int
, wird es eine ganze Zahl.formatter_argparse.RawDescriptionHelpFormatter
angeben.Recommended Posts