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

  • Please clarify and use punctuation. What's "not working"? Show us. – Arya McCarthy May 15 '17 at 05:11
  • how do I store the string(s) and pass it into the functions, sorry new to stackoverflow – Pranav Shankar May 15 '17 at 05:14
  • 1
    can you show your code and expected input / output? – CIsForCookies May 15 '17 at 05: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. – Jim DeLaHunt May 15 '17 at 05: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. – hpaulj May 15 '17 at 16:59
  • `nargs='?'` is also handy. Also keep in mind that the default `default` is `None`, with a reliable `is None` or `is not None` test. – hpaulj May 15 '17 at 17:28

1 Answers1

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

mertyildiran
  • 6,477
  • 5
  • 32
  • 55
  • @CIsForCookies because it turns out he was asking about [argparse optional arguments without dashes](http://stackoverflow.com/questions/11310626/python-argparse-optional-arguments-without-dashes). I thought it was about positional parameters because of the lack of information. The question could be a duplicate... – mertyildiran May 15 '17 at 06:08
  • @CIsForCookies please look at the original version of the question: http://stackoverflow.com/revisions/43971884/2 – mertyildiran May 15 '17 at 06:14
  • @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. – mertyildiran May 15 '17 at 06:24