Set Input Variable at the Start of a Python Script
Question: I'm working on a python script in Red Hat that connect to a SQL database and
run queries on a specific row. What I am attempting to do is to set the row
that I want to query as an input variable that get's passed into the script
when i start it. For example, when I start the script I would like to just run
the command:

    
    
    ./SQLQuery.py Row_To_Query = Row_Name
    

For a similar script that I have in VBScript I would run the wscript command
like this:

    
    
    wscript SQLQuery.vbs /name:Row_Name
    

Then the row name would get passed into the script and the relevant data used
as the script progresses.

My end goal is to run the python script as a task every 10 minuets or so and
pull the relevant data from a specific row, but also have the ability to
specify which row the SQL queries are run against without having to edit the
script each time I want to run it.


Answer: You can use [`argparse`](https://docs.python.org/dev/library/argparse.html) to
parse the commandline arguments given in a standard unix-like format:

    
    
    import argparse
    
    parser = argparse.ArgumentParser()
    parser.add_argument('--row-to-query', type=str, help='row to query')
    namespace = parser.parse_args()
    
    row_name = namespace.row_to_query
    ...
    

You'd then run the program like:

    
    
    ./SQLQuery.py --row-to-query=some_row_name
    

or

    
    
    ./SQLQuery.py --row-to-query some_row_name
    

both invocations should be equivalent.

