command-line-arguments

How to make a short and long version of a required argument using Python Argparse?

How to make a short and long version of a required argument using Python Argparse? Question: I want to specify a required argument called inputdir but I also would like to have a shorthand version of it called i. I don’t see a concise solution to do this without making both optional arguments and then …

Total answers: 2

Why do I need "sys.argv" to start a QApplication in PyQt?

Why do I need "sys.argv" to start a QApplication in PyQt? Question: I try to understand what PyQt does. And one of the first things I didn’t, was: QApplication(sys.argv) Why do I have to give QApplication this argument? I know what sys.argv does. But in my Scripts I wouldn’t need it. Asked By: Sir2B || …

Total answers: 2

How to check if a process with Command line argument is running using python

How to check if a process with Command line argument is running using python Question: I would like to check if a script is running with a specific command line argument within a python script. For example I would like to check if: main.py testarg Is running. Is there any way I can achieve this? …

Total answers: 5

Handle spaces in argparse input

Handle spaces in argparse input Question: Using python and argparse, the user could input a file name with -d as the flag. parser.add_argument(“-d”, “–dmp”, default=None) However, this failed when the path included spaces. E.g. -d C:SMTHNGName with spacesMOREfile.csv NOTE: the spaces would cause an error (flag only takes in ‘C:SMTHNGName’ as input). error: unrecognized arguments: …

Total answers: 7

Parsing boolean values with argparse

Parsing boolean values with argparse Question: I would like to use argparse to parse boolean command-line arguments written as “–foo True” or “–foo False”. For example: my_program –my_boolean_flag False However, the following test code does not do what I would like: import argparse parser = argparse.ArgumentParser(description=”My parser”) parser.add_argument(“–my_bool”, type=bool) cmd_line = [“–my_bool”, “False”] parsed_args = …

Total answers: 26

python argparse – either both optional arguments or else neither one

python argparse – either both optional arguments or else neither one Question: I have a program that uses a default name and password. I’m using argparse to allow the user to specify command line options, and I would like to enable the user to provide the program with a different name and password to use. …

Total answers: 3

Restricting values of command line options

Restricting values of command line options Question: How do I restrict the values of the argparse options? In the below code sau option should only accept a number of 0 or 1 and bg should only allow an integer. How can I implement this? import os import sys, getopt import argparse def main (): parser …

Total answers: 1

Argparse: Check if any arguments have been passed

Argparse: Check if any arguments have been passed Question: My script should start a demo mode, when the no parameters are given. I tried this: args = parser.parse_args() if len(args) == 0: run_demo() else: # evaluate args Which gives a *** TypeError: object of type ‘Namespace’ has no len() as args is no list. How …

Total answers: 8

How to parse multiple nested sub-commands using python argparse?

How to parse multiple nested sub-commands using python argparse? Question: I am implementing a command line program which has interface like this: cmd [GLOBAL_OPTIONS] {command [COMMAND_OPTS]} [{command [COMMAND_OPTS]} …] I have gone through the argparse documentation. I can implement GLOBAL_OPTIONS as optional argument using add_argument in argparse. And the {command [COMMAND_OPTS]} using Sub-commands. From the …

Total answers: 13