You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: appendix/argparse/README.md
+17-1Lines changed: 17 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,5 @@
1
+
# argparse
2
+
1
3
The `argparse` module will interpret all the command-line arguments to your program. I suggest you use `argparse` for every command-line program you write so that you always have a standard way to get arguments and present help.
2
4
3
5
## Types of arguments
@@ -97,6 +99,20 @@ if __name__ == '__main__':
97
99
98
100
The `type` of the input `file` argument is an *open file handle* which we can directly read line-by-line with a `for` loop! Because it's a file *handle* and not a file *name*, I chose to call the variable `fh` to help me remember what it is. You can access the file's name via `fh.name`.
99
101
102
+
````
103
+
$ ./cat_n.py ../../inputs/the-bustle.txt
104
+
Reading "../../inputs/the-bustle.txt"
105
+
0 The bustle in a house
106
+
1 The morning after death
107
+
2 Is solemnest of industries
108
+
3 Enacted upon earth,--
109
+
4
110
+
5 The sweeping up the heart,
111
+
6 And putting love away
112
+
7 We shall not want to use again
113
+
8 Until eternity.
114
+
````
115
+
100
116
## Number of arguments
101
117
102
118
If you want one positional argument, you can define them like so:
As with all the solutions presented, this assumes you have stubbed the program with `new.py` and that you are using the `argparse` module. I suggest putting this logic into a separate function which here is called `get_args` and which I like to define first so that I can see right away when I'm reading the program what the program expects as input. On line 12, I set the `description` for the program that will be displayed with the help documentation. On line 15, I indicate that the program expects just one *positional* argument, no more, no less. Since it is a "word" that I expect, I called the argument `word` which is also how I will access the value on line 25. I use the `metavar` on line 15 to let the user know that this should be a string.
4
+
5
+
The `get_args` function will `return` the result of parsing the command line arguments which I put into the variable `args` on line 24. I can now access the `word` by call `args.word`. Note the lack of parentheses -- it's not `args.word()` -- as this is not a function call. Think of it like a slot where the value lives.
6
+
7
+
On line 26, we need to figure out whether the `article` should be `a` or `an`. We'll use a very simple rule that any word that has a first character that is a vowel should get `an` and otherwise we choose `a`. This obviously misses actual pronunciations like in American English we don't pronounce the "h" in "herb" and so actually say "an herb" whereas the British *do* pronounce the "h" and so would say "an herb". (Even more bizarre to me is that the British leave off the article entirely for the word "hospital" as in, "The Queen is in hospital!") Nor will we consider words where the initial `y` acts like a vowel.
8
+
9
+
We can access the first character of the `word` with `word[0]` which looks the same as how we access the first element of a list. Strings are really list of characters, so this isn't so far-fetched, but we do have to remember that Python, like so many programming languages, starts numbering at `0`, so we often talked about the first element of a list as the "zeroth" element.
10
+
11
+
To decided if the given word starts with a vowel, we ask if `word[0].lower() in 'aeiou'`. So, to unpack that, `word[0]` returns a one-character-long `str` type which has the method `.lower()` which we call using the parentheses. Without the parens, this would just be the *idea* of the function that returns a lowercased version of the string. Understand that the `word` remains unchanged. The function does not lowercase `word[0]`, it only *returns a lowercase version* of that character.
12
+
13
+
The `X in Y` form is a way to ask if element `X` is in the collection `Y`:
14
+
15
+
````
16
+
>>> 'a' in 'abc'
17
+
True
18
+
>>> 'foo' in ['foo', 'bar']
19
+
True
20
+
>>> 3 in range(5)
21
+
True
22
+
>>> 10 in range(3)
23
+
False
24
+
````
25
+
26
+
So we get the first character of `word` and ask if the lowercased version is in the list of characters `aeiou`.
27
+
28
+
The `if`*expression* is different from an `if`*statement*. An expression returns a value, and a statement does not. The `if` expression must have an `else`, but the `if` statement does not have this requirement. The first value is returned if the predicate (the bit after the `if`) evaluates to `True` in a Boolean context (more on that later), otherwise the last value is returned:
0 commit comments