MCPcopy
hub / github.com/fluentpython/example-code-2e / process_args

Function process_args

20-executors/getflags/flags2_common.py:90–144  ·  view source on GitHub ↗
(default_concur_req)

Source from the content-addressed store, hash-verified

88
89
90def process_args(default_concur_req):
91 server_options = ', '.join(sorted(SERVERS))
92 parser = argparse.ArgumentParser(
93 description='Download flags for country codes. '
94 'Default: top 20 countries by population.')
95 parser.add_argument(
96 'cc', metavar='CC', nargs='*',
97 help='country code or 1st letter (eg. B for BA...BZ)')
98 parser.add_argument(
99 '-a', '--all', action='store_true',
100 help='get all available flags (AD to ZW)')
101 parser.add_argument(
102 '-e', '--every', action='store_true',
103 help='get flags for every possible code (AA...ZZ)')
104 parser.add_argument(
105 '-l', '--limit', metavar='N', type=int, help='limit to N first codes',
106 default=sys.maxsize)
107 parser.add_argument(
108 '-m', '--max_req', metavar='CONCURRENT', type=int,
109 default=default_concur_req,
110 help=f'maximum concurrent requests (default={default_concur_req})')
111 parser.add_argument(
112 '-s', '--server', metavar='LABEL', default=DEFAULT_SERVER,
113 help=f'Server to hit; one of {server_options} '
114 f'(default={DEFAULT_SERVER})')
115 parser.add_argument(
116 '-v', '--verbose', action='store_true',
117 help='output detailed progress info')
118 args = parser.parse_args()
119 if args.max_req < 1:
120 print('*** Usage error: --max_req CONCURRENT must be >= 1')
121 parser.print_usage()
122 # "standard" exit status codes:
123 # https://stackoverflow.com/questions/1101957/are-there-any-standard-exit-status-codes-in-linux/40484670#40484670
124 sys.exit(2) # command line usage error
125 if args.limit < 1:
126 print('*** Usage error: --limit N must be >= 1')
127 parser.print_usage()
128 sys.exit(2) # command line usage error
129 args.server = args.server.upper()
130 if args.server not in SERVERS:
131 print(f'*** Usage error: --server LABEL '
132 f'must be one of {server_options}')
133 parser.print_usage()
134 sys.exit(2) # command line usage error
135 try:
136 cc_list = expand_cc_args(args.every, args.all, args.cc, args.limit)
137 except ValueError as exc:
138 print(exc.args[0])
139 parser.print_usage()
140 sys.exit(2) # command line usage error
141
142 if not cc_list:
143 cc_list = sorted(POP20_CC)[:args.limit]
144 return args, cc_list
145
146
147def main(download_many, default_concur_req, max_concur_req):

Callers 1

mainFunction · 0.85

Calls 1

expand_cc_argsFunction · 0.85

Tested by

no test coverage detected