Python argparse Module: Difference between revisions

From GM-RKB
Jump to navigation Jump to search
(Created page with "A Python argparse Module is a command-line argument processing Function for Python programs. * <B>Counter-Example(s):</B> ** Perl GetOpt...")
 
m (Text replacement - "xam ple" to "xample")
 
(10 intermediate revisions by 2 users not shown)
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-Example(s):</B>
* <B>Counter-Example(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]].
----
----
----
----
== References ==
== References ==


=== 2017 ===
=== 2017 ===
* https://docs.python.org/3/library/argparse.html
* 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.
** QUOTE: The [[Python argparse Module|argparse module]] makes it easy to write [[user-friendly]] [[command-line interface]]s. The program defines what arguments it requires, and [[Python argparse Module|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:
** Example. The following code is a Python program that takes a list of integers and produces either the sum or the max:
  import argparse <BR>
  import argparse <BR>
Line 21: Line 24:
  print(args.accumulate(args.integers))
  print(args.accumulate(args.integers))


----


----
__NOTOC__
__NOTOC__
[[Category:Concept]]
[[Category:Concept]]

Latest revision as of 06:56, 7 January 2023

A Python argparse Module is a command-line argument processing Function for Python programs.



References

2017

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))