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: _episodes/07-command_line.md
+71-20Lines changed: 71 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -8,8 +8,9 @@ objectives:
8
8
- "Make code executable from the Linux command line."
9
9
- "Use argparse to accept user inputs."
10
10
keypoints:
11
-
- "You must `import sys` in your code to accept user arguments."
12
-
- "The name of the script itself is always `sys.argv[0]` so the first user input is normally `sys.argv[1]`."
11
+
- "You must `import argparse` in your code to accept user arguments."
12
+
- "You add must first create an argument parser using `parser = argparse.ArgumentParser`"
13
+
- "You add arguments using `parser.add_argument`"
13
14
---
14
15
## Creating and running a python input file
15
16
@@ -80,19 +81,19 @@ import argparse
80
81
81
82
We are importing a library called [https://docs.python.org/3/library/argparse.html](argparse) which can be used to easily make scripts with command line arguments. `Argparse` has the ability to allow us to easily write documentation for our scripts as well.
82
83
83
-
First, we have to tell `argparse` what are arguments are. We tell argparse that we want to add some arguments using the following command:
84
+
We tell argparse that we want to add a command line interface. The syntax for this is
84
85
85
86
~~~
86
87
parser = argparse.ArgumentParser(description="This script analyzes a user given xyz file and outputs the length of the bonds.")
87
88
~~~
88
89
{: .language-python}
89
90
90
-
We've included a description of the script for our users using `description=`.
91
+
We've included a description of the script for our users using `description=`. This description does not need to explain what the arguments are, that will be done automatically for us in the next steps.
91
92
92
93
Next, we have to tell `argparse` what arguments it should expect. In general, the syntax for this is
93
94
94
95
~~~
95
-
parser.add_argument("argument name", help="Your help message for this argument.")
96
+
parser.add_argument("argument_name", help="Your help message for this argument.")
96
97
~~~
97
98
{: .language-python}
98
99
@@ -109,8 +110,7 @@ args = parser.parse_args()
109
110
~~~
110
111
{: .language-python}
111
112
112
-
Our arguments are in the `args` variable. We can get the value of an argument by using `args.ARGUMENT_NAME`, so to get the xyz file the user puts in, we use `args.xyz_file`. Notice that what follows after the dot is the same thing we but in quotation marks when using `add_argument.`
113
-
113
+
Our arguments are in the `args` variable. We can get the value of an argument by using `args.argument_name`, so to get the xyz file the user puts in, we use `args.xyz_file`. Notice that what follows after the dot is the same thing we but in quotation marks when using `add_argument.`
Check that the output of your code is what you expected.
152
152
153
-
154
153
What would happen if the user forgot to specify the name of the xyz file?
155
154
156
155
~~~
157
156
usage: analyze.py [-h] xyz_file
158
157
analyze.py: error: the following arguments are required: xyz_file
159
158
~~~
160
-
{: .output}
159
+
{: .error}
161
160
162
161
Argparse handles this for us and prints an error message. It tells us that we must specifiy an xyz file.
163
162
164
-
## Optional Arguments
163
+
Try out your program with other XYZ files in your `data` folder.
164
+
165
+
## The "main" part of our script
166
+
We need to add one more thing to our code. When you write a code that includes function definitions and a main script, you need to tell python which part is the main script. (This becomes very important later when we are talking about testing.) *After* your import statements and function definitions and *before* use `argparse`
167
+
```
168
+
if __name__ == "__main__":
169
+
```
170
+
{: .language-python}
171
+
172
+
Since this is an `if` statement, you now need to indent each line of your main script below this if statement. Be very careful with your indentation! Don't use a mixture of tabs and spaces! A good way to indent multiple lines in many text editors is to highlight the lines you would like to indent, then press `tab`.
173
+
174
+
Save your code and run it again. It should work exactly as before. If you now get an error message, it is probably due to inconsistent indentation.
if bond_check(bond_length_12, minimum_length=args.minimum_length, maximum_length=args.maximum_length) is True:
218
+
print(F'{symbols[num1]} to {symbols[num2]} : {bond_length_12:.3f}')
219
+
~~~
220
+
{: .language-python}
221
+
222
+
## Extension - Optional Arguments
165
223
What's another argument we might want to include? We also might want to let the user specify a minimum and maximum bond length on the command line. We would want these to be optional, just like they are in our function.
166
224
167
225
We can add optional arguments by putting a dash (`-`) or two dashes (`--`) in front of the argument name when we add an argument. Add this line below where you added the fist argument. Note that all `add_argument` lines should be above the line with `parse_args`.
@@ -200,16 +258,7 @@ if bond_check(bond_length_12, minimum_length=args.minimum_length, maximum_length
200
258
~~~
201
259
{: .language-python}
202
260
203
-
## The "main" part of our script
204
-
We need to add one more thing to our code. When you write a code that includes function definitions and a main script, you need to tell python which part is the main script. (This becomes very important later when we are talking about testing.) *After* your import statements and function definitions and *before* use `argparse`
205
-
```
206
-
if __name__ == "__main__":
207
-
```
208
-
{: .language-python}
209
-
210
-
Since this is an `if` statement, you now need to indent each line of your main script below this if statement. Be very careful with your indentation! Don't use a mixture of tabs and spaces! A good way to indent multiple lines in many text editors is to highlight the lines you would like to indent, then press `tab`.
211
-
212
-
Save your code and run it again. It should work exactly as before. If you now get an error message, it is probably due to inconsistent indentation.
0 commit comments