Skip to content

Instantly share code, notes, and snippets.

@dideler
Last active August 17, 2025 06:15
Show Gist options
  • Save dideler/2395703 to your computer and use it in GitHub Desktop.
Save dideler/2395703 to your computer and use it in GitHub Desktop.

Revisions

  1. dideler revised this gist Nov 30, 2018. 1 changed file with 27 additions and 21 deletions.
    48 changes: 27 additions & 21 deletions pyargs.md
    Original 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:

    import sys
    print(sys.argv) # Note the first argument is always the script filename.
    ```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.

    """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)
    ```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:

    $ python main.py -i input.txt -o output.txt
    input.txt
    {'-o': 'output.txt', '-i': 'input.txt'}
    ```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:

  2. dideler revised this gist Apr 6, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions pyargs.md
    Original 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)
    * `argparse` (recommended)
    * `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.
  3. dideler revised this gist Dec 21, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pyargs.md
    Original 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`
    * `argparse` (recommended)

    This post was inspired by the book "Programming Python" by Mark Lutz.
  4. dideler revised this gist Jul 11, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions pyargs.md
    Original 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.
    while argv: # While there are arguments left to parse.
    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.
    argv = argv[1:] # Reduce the argument list by copying it starting from index 1.
    return opts

    if __name__ == '__main__':
  5. dideler revised this gist Jul 11, 2012. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions pyargs.md
    Original 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.
    else:
    argv = argv[1:]
    argv = argv[1:] # Reduce the argument list.
    return opts

    if __name__ == '__main__':
  6. dideler revised this gist Apr 16, 2012. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions pyargs.md
    Original 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)
    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 (note the first argument is always the script filename):
    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), and `argparse`.
    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.
  7. dideler created this gist Apr 16, 2012.
    39 changes: 39 additions & 0 deletions pyargs.md
    Original 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.