Skip to content

Instantly share code, notes, and snippets.

@iRajul
Forked from dideler/pyargs.md
Created March 24, 2018 13:47
Show Gist options
  • Select an option

  • Save iRajul/0c7d8ac3648545187ac2d5e3b40d0f2f to your computer and use it in GitHub Desktop.

Select an option

Save iRajul/0c7d8ac3648545187ac2d5e3b40d0f2f to your computer and use it in GitHub Desktop.

Revisions

  1. @dideler 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.
  2. @dideler 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.
  3. @dideler 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__':
  4. @dideler 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__':
  5. @dideler 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.
  6. @dideler 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.