I am using argparse to get command line arguments like:
parser = argparse.ArgumentParser()
parser.add_argument('execCmd' , type=str)
args = parser.parse_args()
print(args)
Now when I run it from command line like this:
$python script.py "/bin/exec 10"
The result on the console is
Namespace(execCmd='C:/Program Files/usr/bin/exec 10')
It prepends the file path to the argument. The user may start the command with a '/' or they may not. Is there a way to handle this in argeparse such that the command is passed as it is without the file directory if the user adds '/'?
Anyways, argparse can take in functions for the type and you can do something like a check to see if the first character of what they pass in is a /
or not.
So like:
def check_slash(string):
if string and len(string) > 0 and string[0] == '/':
return string
else:
return 'C:/Program Files/' + string
parser.add_argument('execCmd' , type=check_slash)
The issue is with bash as defined here: https://github.com/bmatzelle/gow/issues/196
I was running this from git bash and therefore it was appending the gow path to the argument. Passing the argument as
$python script.py "//bin\exec 10"
works and so does leaving a space
$python script.py " /bin/exec 10"
This is not an issue if same command is run in powershell.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.