From 9ad28915e76887cd55d64e977e603d058a85ba3a Mon Sep 17 00:00:00 2001 From: Ben Ward Date: Mon, 13 May 2024 14:44:53 -0500 Subject: [PATCH 1/2] revise structure and add notes file --- lesson-2/lesson-2.py | 5 +++++ lesson-2/notes_2.txt | 2 ++ 2 files changed, 7 insertions(+) create mode 100644 lesson-2/lesson-2.py create mode 100644 lesson-2/notes_2.txt diff --git a/lesson-2/lesson-2.py b/lesson-2/lesson-2.py new file mode 100644 index 0000000..8545b51 --- /dev/null +++ b/lesson-2/lesson-2.py @@ -0,0 +1,5 @@ +import argparse +parser = argparse.ArgumentParser() +parser.add_argument("echo") +args = parser.parse_args() +print(args.echo) diff --git a/lesson-2/notes_2.txt b/lesson-2/notes_2.txt new file mode 100644 index 0000000..0ce1c01 --- /dev/null +++ b/lesson-2/notes_2.txt @@ -0,0 +1,2 @@ +# argparse tutorial + From 17e3bf4997b28650092d2ab5280c36bdce8e4077 Mon Sep 17 00:00:00 2001 From: Ben Ward Date: Mon, 20 May 2024 12:57:23 -0500 Subject: [PATCH 2/2] lesson in progress --- lesson-2/lesson-2.py | 6 +----- lesson-2/notes_2.txt | 22 ++++++++++++++++++++++ lesson-2/test-prog.py | 10 ++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 lesson-2/test-prog.py diff --git a/lesson-2/lesson-2.py b/lesson-2/lesson-2.py index 8545b51..8b13789 100644 --- a/lesson-2/lesson-2.py +++ b/lesson-2/lesson-2.py @@ -1,5 +1 @@ -import argparse -parser = argparse.ArgumentParser() -parser.add_argument("echo") -args = parser.parse_args() -print(args.echo) + diff --git a/lesson-2/notes_2.txt b/lesson-2/notes_2.txt index 0ce1c01..6c92619 100644 --- a/lesson-2/notes_2.txt +++ b/lesson-2/notes_2.txt @@ -1,2 +1,24 @@ # argparse tutorial +- there are two modules that fulfill the same task, `getopt` (an equivalent for `getop()` from the C lang) +and the deprecated `optparse`. + +importing argparse and having no options will do nothing, but a simple help message is auto generated + +positional arguments + +`add_argument()` method, specifies which command-line options the program is willing to accept +`parse_args()` method returns data from the options specified + +argparse will treat options as strings, unless told otherwise; for instance, `type=int` + +optional arguments, are not necessary to run the program without errors + +since it's optional, it is given the value of `None`, and will need to be given a new value to fulfill the +argument requirements + +you can make the optional argument more of a flag for true/false by providing a stored value when the command/flag +is called, i.e. parser.add_argument("--verbose", help="increase output verbosity", action="store_true") + +the options can be shortened for ease of use, +e.g. parser.add_argument("-v", "--verbose", help="increase output verbosity",action="store_true") \ No newline at end of file diff --git a/lesson-2/test-prog.py b/lesson-2/test-prog.py new file mode 100644 index 0000000..816069f --- /dev/null +++ b/lesson-2/test-prog.py @@ -0,0 +1,10 @@ +import argparse +parser = argparse.ArgumentParser() +parser.add_argument("square", type=int, help="display a square of a given number") +parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") +args = parser.parse_args() +answer = args.square**2 +if args.verbose: + print(f"the square of {args.square} equals {answer}") +else: + print(answer) \ No newline at end of file