Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in progress lesson 2 work #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lesson-2/lesson-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

24 changes: 24 additions & 0 deletions lesson-2/notes_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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")
10 changes: 10 additions & 0 deletions lesson-2/test-prog.py
Original file line number Diff line number Diff line change
@@ -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)