Verified Commit 8e467b0f authored by Toby Hodges's avatar Toby Hodges
Browse files

tag bash & output blocks

parent d03884e1
......@@ -45,6 +45,9 @@ command used to run the program was `split` by whitespace:
~~~
$ cat argv_example1.py
~~~
{: .bash }
~~~
from sys import argv
print('START')
......@@ -52,7 +55,13 @@ print(argv)
for arg in argv:
print(arg)
print('END')
~~~
{: .output }
~~~
$ python argv_example1.py one 2 3.0 four,five
~~~
{: .bash }
~~~
START
argv_example1.py one 2 3.0 four,five
argv_example1.py
......@@ -62,6 +71,7 @@ one
four,five
END
~~~
{: .output }
> ## Carry the Zero
>
......@@ -79,19 +89,29 @@ One important thing to note about `sys.argv` is that all the values within the l
~~~
$ cat argv_example2.py
~~~
{: .bash }
~~~
from sys import argv
print('START')
for arg in argv[1:]:
print(arg + 3)
print('END')
~~~
{: .output }
~~~
$ python argv_example2.py 1 2 3
~~~
{: .bash }
~~~
START
Traceback (most recent call last):
File "argv_example2.py", line 5, in <module>
print(arg + 3)
TypeError: can only concatenate str (not "int") to str
~~~
{: .output }
> ## Options & Arguments
> The code block below shows the start of a program
......@@ -164,17 +184,26 @@ we can already see one big advantage of using `argparse`:
~~~
$ cat argparse_example1.py
~~~
{: .bash }
~~~
import argparse
parser=argparse.ArgumentParser()
parser.parse_args()
~~~
{: .output }
~~~
$ python argparse_example1.py -h
~~~
{: .bash }
~~~
usage: argparse_example1.py [-h]
optional arguments:
-h, --help show this help message and exit
~~~
{: .output }
That's right: `argparse` automatically generates a usage/help message
to be displayed for the program,
......@@ -195,13 +224,22 @@ with the aptly-named `add_argument` method.
~~~
$ cat argparse_example2.py
~~~
{: .bash }
~~~
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("input_file",
help="path to an input file for processing")
parser.parse_args()
~~~
{: .output }
~~~
$ python argparse_example2.py
~~~
{: .bash }
~~~
usage: argparse_example2.py [-h] input_file
positional arguments:
......@@ -210,7 +248,7 @@ positional arguments:
optional arguments:
-h, --help show this help message and exit
~~~
{: .output }
The example above shows how the program's help message
has been updated to list this positional argument,
......@@ -220,10 +258,13 @@ input file path?
~~~
$ python argparse_example2.py
~~~
{: .bash }
~~~
usage: argparse_example2.py [-h] input_file
argparse_example2.py: error: the following arguments are required: input_file
~~~
{: .output }
The program raises an error (code 2)
prints a short description of the intended usage of the script,
......@@ -238,6 +279,9 @@ This is best understood with another example:
~~~
$ cat argparse_example3.py
~~~
{: .bash }
~~~
import argparse
parser=argparse.ArgumentParser()
......@@ -246,10 +290,16 @@ parser.add_argument("input_file",
args = parser.parse_args()
print(f"input_file provided was {args.input_file}")
~~~
{: .output }
~~~
$ python argparse_example3.py path/to/input.file
~~~
{: .bash }
~~~
input_file provided was path/to/input.file
~~~
{: .output }
> ## Adding More Arguments
>
......@@ -311,6 +361,9 @@ we can use the `type` parameter of `add_argument`:
~~~
$ cat argparse_example5.py
~~~
{: .bash }
~~~
import argparse
parser=argparse.ArgumentParser()
......@@ -322,11 +375,17 @@ args = parser.parse_args()
print(f"input_file provided was {args.input_file}")
print(args.number1 + args.number2)
~~~
{: .output }
~~~
$ python argparse_example5.py path/to/input.file 5 70
~~~
{: .bash }
~~~
input_file provided was path/to/input.file
75
~~~
{: .output }
The value provided to this `type` parameter must be a function
that converts a string value to some other data type,
......@@ -383,14 +442,23 @@ the three positional arguments:
~~~
$ python argparse_example6.py input.txt 2 3
~~~
{: .bash }
~~~
input_file provided was input.txt
5
~~~
{: .output }
~~~
$ python argparse_example6.py input.txt 2 3 --output=output.txt
~~~
{: .bash }
~~~
input_file provided was input.txt
$ cat output.txt
5
~~~
{: .output }
Configured this way, the optional field still expects a value to be provided
i.e. `output` is an _optional argument_.
......@@ -430,9 +498,13 @@ else:
{: .language-python }
~~~
$ python argparse_example7.py input.txt 2 3 --loud
~~~
{: .bash }
~~~
INPUT_FILE PROVIDED WAS INPUT.TXT
5
~~~
{: .output }
### Restricting Input Values
......@@ -489,6 +561,9 @@ else:
~~~
$ python argparse_example8.py -h
~~~
{: .bash }
~~~
usage: argparse_example8.py [-h] [--output OUTPUT]
input_file number1 number2 {en,de,es,pt}
......@@ -502,14 +577,27 @@ optional arguments:
-h, --help show this help message and exit
--output OUTPUT, -o OUTPUT
path to an output file (default: print to STDOUT)
~~~
{: .output }
~~~
$ python argparse_example8.py input.txt 2 3 pt
~~~
{: .bash }
~~~
nome do arquivo de entrada: input.txt
5
~~~
{: .output }
~~~
$ python argparse_example8.py input.txt 2 3 fr
~~~
{: .bash }
~~~
usage: argparse_example8.py [-h] [--output OUTPUT]
input_file number1 number2 {en,de,es,pt}
argparse_example8.py: error: argument language: invalid choice: 'fr' (choose from 'en', 'de', 'es', 'pt')
~~~
{: .output }
### Default Values
......@@ -554,12 +642,22 @@ args = parser.parse_args()
{: .language-python }
~~~
$ python argparse_example9.py -l pt input.txt 2 3
~~~
{: .bash }
~~~
nome do arquivo de entrada: input.txt
5
~~~
{: .output }
~~~
$ python argparse_example9.py input.txt 2 3
~~~
{: .bash }
~~~
input file: input.txt
5
~~~
{: .output }
### Capturing Multiple Values
......@@ -699,6 +797,9 @@ with the mutually exclusive options separated by a `|` pipe.
~~~
$ python argparse_example11.py -h
~~~
{: .bash }
~~~
usage: argparse_example11.py [-h] [--language {en,de,es,pt}] [--output OUTPUT]
[--verbose | --silent]
input_file numbers [numbers ...]
......@@ -716,19 +817,30 @@ optional arguments:
--verbose, -v
--silent, -q
~~~
{: .output }
Let's check that these flags work as intended:
~~~
$ python argparse_example11.py --silent input.txt 2 3 4
~~~
{: .bash }
~~~
9
~~~
{: .output }
~~~
$ python argparse_example11.py -v -o output.txt input.txt 2 3 4
~~~
{: .bash }
~~~
Starting...
input file: input.txt
Calculating result...
Writing result to file...
Finishing...
~~~
{: .output }
> ## Recommended Reading
>
......@@ -773,7 +885,9 @@ when we ask for the help message with `-h`.
~~~
$ python argparse_example12.py -h
python argparse_example12.py -h
~~~
{: .bash }
~~~
usage: argparse_example12.py [-h] [--language {en,de,es,pt}] [--output OUTPUT]
[--verbose | --silent]
input_file numbers [numbers ...]
......@@ -794,6 +908,7 @@ optional arguments:
--verbose, -v
--silent, -q
~~~
{: .output }
> ## Parse Master
> Using `argparse`, write a script `process_files.py`,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment