Passing Strings as Python command-line arguments

Iโ€™m able to pass ints and chars as Python command-line args, but canโ€™t pass Strings successfully to the following:

if __name__ == '__main__' :
    try :
        print(len(sys.argv))
        arg1 = ast.literal_eval(sys.argv[1])
        arg2 = ast.literal_eval(sys.argv[2])
        print(arg1)
        print(arg2)
    except Exception :
        print('error')

The following works:

python test.py 1 2

and prints:

3
1
2

as dos the following:

python test.py โ€˜1โ€™ โ€˜2โ€™

which also prints

3
1
2

However, the following does not work:

python test.py โ€˜oneโ€™ โ€˜twoโ€™

It sees the number of args, but throws an Exception trying to grab them:

3
error

Could someone please provide some guidance on how to pass Strings as command-line args?

Thanks.


Asked by: Leonardo904 | Posted: 30-11-2021






Answer 1

Your shell will eat those quotes before python ever sees them. Try like this:

python test.py '"one"' '"two"'

As an aside, command-line args are passed as strings anyway. It is odd to literal_eval them like this; a better pattern is to convert the strings to numbers when you need the numbers, and to just leave them as strings otherwise. This will also save you from having to quote-pad the strings twice on the command line.

The argparse module can handle that stuff for you automatically.

Answered by: Robert884 | Posted: 01-01-2022



Answer 2

optparse.OptionParser may be helpful for you.

You can use it like this:

from optparse import OptionParser
if __name__ == "__main__":
    parser = OptionParser(version="%prog 1.0.0")
    parser.add_option("-c", "--config", action="store", dest="config_file",
                      default="test", type="string",
                      help="let prog load a specified configure file")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
                      default=False, help="let prog run in verbose model")
    options, args = parser.parse_args()
    if options.verbose:
        print "run in verbose model"
    print "configure file is %s"%(options.config_file)

Answered by: Adrian961 | Posted: 01-01-2022



Similar questions

python - Passing Command-line Arguments in C++

I've been trying to learn how to extend Python 3 with C++, and I was recommended using Boost. I believe I've followed the procedure of setting up Boost::Python correctly so far, and I have the following code from here (saved as example.cpp) which builds successfully: #incl...


python - How to prompt for user input and read command-line arguments

Closed. This question needs to be more focused. It ...


python - command-line options and arguments using getopt

I'm trying to write a piece of code in python to get command-line options and arguments using getopt module. Here is my code: import getopt import sys def usage (): print('Usage') def main(): try: opts, args = getopt.getopt(sys.argv[1:], 'xy:') except getopt.GetoptError as err: print(err) usage() sys.exit() for o,a in opts: if o in ("-x", "--xxx"):...


ruby on rails - UTF-8 minus sign rejected in python command-line arguments

I am running python 2.6 on Ubuntu Lucent and having trouble getting the minus sign in negative command-line arguments to be interpreted properly, especially when the call to the script is initiated through the OS via Rails (using backquotes). In particular, the minus sign seems to be coming in as UTF-8. When command-line arguments are interpreted manually, as in: lng = float(sys.argv[4])


python - Passing Command-line Arguments in C++

I've been trying to learn how to extend Python 3 with C++, and I was recommended using Boost. I believe I've followed the procedure of setting up Boost::Python correctly so far, and I have the following code from here (saved as example.cpp) which builds successfully: #incl...


python - How can I get the command-line arguments in an embedded Pig script?

I'm writing an embedded pig script in python, and I'd like to pass arguments like this: $ pig myscript.py arg1 arg2 I'd expect sys.argv to be ['myscript.py', 'arg1', 'arg2'], but it's empty. Any idea how I can pass command-line arguments to an embedded pig script?


python - Parsing command-line arguments similar to archlinux pacman

I'm creating a python script with usage in the same style as pacman in Arch Linux, summarized by: prog <operation> [options] [targets] operations are of the form -X (hyphen, uppercase letter), and one is required when calling the script. options are of the form -x ...


How to access command-line arguments from a Python script?

I am writing code in Python that I should run with the command line and when I call the script i should give some arguments that I would use in the code. What can I use to achieve that? To run the script it would be something like this: python myscript.py s1 s2 s4 where s1, s2 and s4 would be the arguments that I would use in my code.


Parsing command-line arguments in Python: array of variable length?

is it possible to create an array in Python with an variable length? I'm getting different kind of length from sys.argv when I start the program and I need as much array field as long as the string from sys.argv is. EDIT for better understanding: I wrote a python script which can controle the GPIO Ports of a Raspberry Pi Until now, the script is just able to control one port at the same time...


Can I pass command-line arguments to Abaqus python?

I have an abaqus python script I've been using for a parametric study. It is exhausting to go into the code and edit it every time I want to run different options. I'd like to be able to pass the script arguments so that I can run different options without needing to change the code. I'd like to do something like this... abaqus cae script='Script.py --verbose --data=someData.dat' I...


python - Provide command-line arguments for function in file

This question already has answers here:


linux - How to make a python, command-line program autocomplete arbitrary things NOT interpreter

I am aware of how to setup autocompletion of python objects in the python interpreter (on unix). Google shows many hits for explanations on how to do this. Unfortunately, there are so many references to that it is difficult to find what I need to do, which is slightly different. I need to know how to enable, tab/auto completion of arbitrary items in a command-line program written in ...


java - OCSP command-line test tool?

Closed. This question does not meet Stack Overflow guid...


python - How to prompt for user input and read command-line arguments

Closed. This question needs to be more focused. It ...


eclipse - Integrating command-line generated python .coverage files with PyDev

My build environment is configured to compile, run and create coverage file at the command line (using Ned Batchelder coverage.py tool). I'm using Eclipse with PyDev as my editor, but for practical reasons, it's not possible/convenient for me to convert my whole build environment to Eclipse (and thus generate the coverage data directly from the IDE, as it's designed to do) PyDev seems to be using the same ...


c++ - How do I compile a Visual Studio project from the command-line?

I'm scripting the checkout, build, distribution, test, and commit cycle for a large C++ solution that is using Monotone, CMake, Visual Studio Express 2008, and custom tests. All of the other parts seem pretty straight-forward, but I don't see how to compile the Visu...


How to make a simple command-line chat in Python?

I study network programming and would like to write a simple command-line chat in Python. I'm wondering how make receving constant along with inputing available for sending at any time. As you see, this client can do only one job at a time: from socket import * HOST = 'localhost' PORT = 21567 BUFSIZE = 1024 ADDR = (HOST, PORT) tcpCliSock = socket(AF_INET, SOCK_STREAM) tcpCliSock.connect(A...


interpreter - Persistent Python Command-Line History

I'd like to be able to "up-arrow" to commands that I input in a previous Python interpreter. I have found the readline module which offers functions like: read_history_file, write_history_file, and set_startup_hook. I'm not quite savvy enough to put this into practice though, so could someone please help? My thoughts on the solution are: (1) Modify .login PYTHO...


data structures - Processing command-line arguments in prefix notation in Python

I'm trying to parse a command-line in Python which looks like the following: $ ./command -o option1 arg1 -o option2 arg2 arg3 In other words, the command takes an unlimited number of arguments, and each argument may optionally be preceded with an -o option, which relates specifically to that argument. I think this is called a "prefix notation". In the Bourne shell I wo...


Display constantly updating information in-place in command-line window using python?

I am essentially building a timer. I have a python script that monitors for an event and then prints out the seconds that have elapsed since that event. Instead of an ugly stream of numbers printed to the command line, I would like to display only the current elapsed time "in-place"-- so that only one number is visible at any given time. Is there a simple way to do this? If possible I'd like to u...


python - How to write script output to file and command-line?

I have a long-running Python script that I run from the command-line. The script writes progress messages and results to the standard output. I want to capture everything the script write to the standard output in a file, but also see it on the command line. Alternatively, I want the output to go to the file immediately, so I can use tail to view the progress. I have tried this: python MyLongRu...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top