Python argparse Module: Difference between revisions
Jump to navigation
Jump to search
m (Text replacement - ". ----" to ". ----") |
m (Text replacement - ". * <B>Counter-Exam" to ". ** … * <B>Counter-Exam ") |
||
Line 1: | Line 1: | ||
A [[Python argparse Module]] is a [[command-line argument processing]] [[programming function|Function]] for [[Python program]]s. | A [[Python argparse Module]] is a [[command-line argument processing]] [[programming function|Function]] for [[Python program]]s. | ||
* <B>Counter- | ** … | ||
* <B>Counter-Exam | |||
ple(s):</B> | |||
** [[Perl GetOptions Subroutine]]. | ** [[Perl GetOptions Subroutine]]. | ||
* <B>See:</B> [[Command-line Option]], [[Command-line Options Processor]]. | * <B>See:</B> [[Command-line Option]], [[Command-line Options Processor]]. |
Revision as of 19:40, 6 January 2023
A Python argparse Module is a command-line argument processing Function for Python programs.
- …
- Counter-Exam
ple(s):
References
2017
- https://docs.python.org/3/library/argparse.html
- QUOTE: The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.
- Example. The following code is a Python program that takes a list of integers and produces either the sum or the max:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)')
args = parser.parse_args() print(args.accumulate(args.integers))