2

So I have a file abc.py

--a string
--b string

as optional arguments I want to be able to do

abc.py string --> func1
abc.py string --a string --> func1 and func2
abc.py string --a string --> func1 and func2
abc.py --a string --> func2

and so on I managed to get --a and --b working (separately and together) I am not able to be abc.py string working am I supposed to use argv and argparse in conjunction?

edit: I guess my question is I want to handle the case when default does not have any argument, i.e. I run --> abc.py --a hello<-- and I need them to run in any combination (none specified, default and a specified, a and b specified, only default specified etc)

if __name__ == '__main__':
parser=argparse.ArgumentParser()
parser.add_argument("default", help="default")
parser.add_argument("--a","-a", help="a")
parser.add_argument("--b","-b", help="b")
args=parser.parse_args()
if args.a:
    a_func(args.a)                   
if args.b:
    b_func(args.b)
default_func(args.default)

edit: okay guys I got it working, what I did was

parser=argparse.ArgumentParser()
parser.add_argument("default",nargs="*", help="default")
parser.add_argument("--a","-a",nargs="*", help="a")
parser.add_argument("--b","-b",nargs="*", help="b")
args=parser.parse_args()
a_func(args.a)                   
b_func(args.b)
default_func(args.default)

Now I just check if the list is empty or not inside the function and I can process multiple arguments in the func as well

6
  • Please clarify and use punctuation. What's "not working"? Show us. Commented May 15, 2017 at 5:11
  • how do I store the string(s) and pass it into the functions, sorry new to stackoverflow Commented May 15, 2017 at 5:14
  • 1
    can you show your code and expected input / output? Commented May 15, 2017 at 5:19
  • @PranavShankar It would be helpful for you to post your solution as an answer, rather than as part of the question. This lets you mark the question as "answered", and lets other people learn more easily from your answer. Commented May 15, 2017 at 5:55
  • Your initial specification was unclear. abc.py --a string --> func2 is the use of -a correct; or do you mean -b. You don't give an example of what -b would trigger. I strongly suggest adding a print(args) so you can see what different inputs (and add_argument definitions) produce. Commented May 15, 2017 at 16:59

1 Answer 1

1

You should you sys module of Python standard libraries:

#!/usr/bin/env python

import sys

print sys.argv[1] // first argument

Which will print string output on the command line in your case.

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string. sys.argv

Sign up to request clarification or add additional context in comments.

3 Comments

@CIsForCookies because it turns out he was asking about argparse optional arguments without dashes. I thought it was about positional parameters because of the lack of information. The question could be a duplicate...
@CIsForCookies please look at the original version of the question: stackoverflow.com/revisions/43971884/2
@CIsForCookies I started to write my answer according to that version of his question and I finished and clicked "Post Your Answer". Then page refreshed and I saw the updated version, but it was too late. I could delete my answer but deleting an answer causing penalties within the Stack Overflow's business logic so because of that I don't want to delete my answer. I can not update my answer either because I'm not sure what he is actually asking, anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.