Last active
August 17, 2025 06:15
-
-
Save dideler/2395703 to your computer and use it in GitHub Desktop.
Revisions
-
dideler revised this gist
Nov 30, 2018 . 1 changed file with 27 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,35 +2,41 @@ Command-line arguments in Python show up in `sys.argv` as a list of strings (so For example, if you want to print all passed command-line arguments: ```python import sys print(sys.argv) # Note the first argument is always the script filename. ``` Command-line options are sometimes passed by position (e.g. `myprogram foo bar`) and sometimes by using a "-name value" pair (e.g. `myprogram -a foo -b bar`). Here's a simple way to parse command-line pair arguments. It scans the `argv` list looking for `-optionname optionvalue` word pairs and places them in a dictionary for easy retrieval. The code is heavily commented to help Python newcomers. ```python """Collect command-line options in a dictionary""" def getopts(argv): opts = {} # Empty dictionary to store key-value pairs. while argv: # While there are arguments left to parse... if argv[0][0] == '-': # Found a "-name value" pair. opts[argv[0]] = argv[1] # Add key and value to the dictionary. argv = argv[1:] # Reduce the argument list by copying it starting from index 1. return opts if __name__ == '__main__': from sys import argv myargs = getopts(argv) if '-i' in myargs: # Example usage. print(myargs['-i']) print(myargs) ``` Running this script: ```shell $ python main.py -i input.txt -o output.txt input.txt {'-o': 'output.txt', '-i': 'input.txt'} ``` Simple solution, but not very robust; it doesn't handle error checking and the like. So don't use this in production code! There are more complex alternatives available. Some modules to consider are: -
dideler revised this gist
Apr 6, 2013 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,7 +35,8 @@ Running this script: Simple solution, but not very robust; it doesn't handle error checking and the like. So don't use this in production code! There are more complex alternatives available. Some modules to consider are: * `getopt` * `optparse` (deprecated since Python 2.7) * `argparse` (recommended if you want something in the standard library) * [`docopt`](http://docopt.org/) (recommended if you're willing to use something not in the standard library) This post was inspired by the book "Programming Python" by Mark Lutz. -
dideler revised this gist
Dec 21, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,6 +36,6 @@ Simple solution, but not very robust; it doesn't handle error checking and the l * `getopt` * `optparse` (deprecated) * `argparse` (recommended) This post was inspired by the book "Programming Python" by Mark Lutz. -
dideler revised this gist
Jul 11, 2012 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,11 +12,11 @@ Here's a simple way to parse command-line pair arguments. It scans the `argv` li """Collect command-line options in a dictionary""" def getopts(argv): opts = {} # Empty dictionary to store key-value pairs. while argv: # While there are arguments left to parse... if argv[0][0] == '-': # Found a "-name value" pair. opts[argv[0]] = argv[1] # Add key and value to the dictionary. argv = argv[1:] # Reduce the argument list by copying it starting from index 1. return opts if __name__ == '__main__': -
dideler revised this gist
Jul 11, 2012 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,9 +16,7 @@ Here's a simple way to parse command-line pair arguments. It scans the `argv` li while argv: # While there are arguments left to parse. if argv[0][0] == '-': # Found a "-name value" pair. opts[argv[0]] = argv[1] # Add key and value to the dictionary. argv = argv[1:] # Reduce the argument list. return opts if __name__ == '__main__': -
dideler revised this gist
Apr 16, 2012 . 1 changed file with 7 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ Command-line arguments in Python show up in `sys.argv` as a list of strings (so For example, if you want to print all passed command-line arguments: import sys print(sys.argv) # Note the first argument is always the script filename. Command-line options are sometimes passed by position (e.g. `myprogram foo bar`) and sometimes by using a "-name value" pair (e.g. `myprogram -a foo -b bar`). @@ -28,12 +28,16 @@ Here's a simple way to parse command-line pair arguments. It scans the `argv` li print(myargs['-i']) print(myargs) Running this script: $ python main.py -i input.txt -o output.txt input.txt {'-o': 'output.txt', '-i': 'input.txt'} Simple solution, but not very robust; it doesn't handle error checking and the like. So don't use this in production code! There are more complex alternatives available. Some modules to consider are: * `getopt` * `optparse` (deprecated) * `argparse` This post was inspired by the book "Programming Python" by Mark Lutz. -
dideler created this gist
Apr 16, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ Command-line arguments in Python show up in `sys.argv` as a list of strings (so you'll need to import the `sys` module). For example, if you want to print all passed command-line arguments: import sys print(sys.argv) Command-line options are sometimes passed by position (e.g. `myprogram foo bar`) and sometimes by using a "-name value" pair (e.g. `myprogram -a foo -b bar`). Here's a simple way to parse command-line pair arguments. It scans the `argv` list looking for `-optionname optionvalue` word pairs and places them in a dictionary for easy retrieval. The code is heavily commented to help Python newcomers. """Collect command-line options in a dictionary""" def getopts(argv): opts = {} # Empty dictionary. while argv: # While there are arguments left to parse. if argv[0][0] == '-': # Found a "-name value" pair. opts[argv[0]] = argv[1] # Add key and value to the dictionary. argv = argv[1:] # Reduce the argument list. else: argv = argv[1:] return opts if __name__ == '__main__': from sys import argv myargs = getopts(argv) if '-i' in myargs: # Example usage. print(myargs['-i']) print(myargs) Running this script (note the first argument is always the script filename): $ python main.py -i input.txt -o output.txt input.txt {'-o': 'output.txt', '-i': 'input.txt'} Simple solution, but not very robust; it doesn't handle error checking and the like. So don't use this in production code! There are more complex alternatives available. Some modules to consider are `getopt`, `optparse` (deprecated), and `argparse`. This post was inspired by the book "Programming Python" by Mark Lutz.