0

I'm not able to find any examples to parse the arguments in the way I need to. The names that have a : in them are a known list (30 of them), the value after them may or may not have values, however some are required.

creatAlert.py call_type: I alert_id: 25 message: STATUS OF AGENT PLATFORM notes:

So I have tried to just parse the call_type: I with no luck. What am I missing to get 'I' in the call_type variable?

parser = argparse.ArgumentParser()
parser.add_argument("call_type:", type=str, help="Testing")
args = parser.parse_args()
print args.call_type

Is there a special trick required to handle the message argument that has spaces in it?

3
  • 1
    Why do you say you need to take arguments in this highly unusual format? Commented Jul 5, 2018 at 16:10
  • The fact is this vendor product is what calls my script as a user exit. I would change it if I could, but I can't so I have to live with what I get. Commented Jul 5, 2018 at 16:16
  • I don't think argparse can handle this. You'll need to roll your own code. Commented Jul 5, 2018 at 16:20

2 Answers 2

1

argparse has expectations that differ from the format provided. If your upstream application doesn't follow argparse conventions, then argparse isn't going to work easily.

Instead, I recommend that you take the entire input line as a string. Search for the colons and divide the line at those words, putting the resulting values into a dictionary. That much is easy (enough) to do with brute force.

  • Find the first word with a colon; make that the dict key
  • Concatenate words into that key's value until you hit EOL, or the next word with a colon.
Sign up to request clarification or add additional context in comments.

Comments

0

With

import sys
print(sys.argv)

2328:~/mypy$ python3 stack51195799.py call_type: I alert_id: 25 message: STATUS OF AGENT PLATFORM notes:
['stack51195799.py', 'call_type:', 'I', 'alert_id:', '25', 'message:', 'STATUS', 'OF', 'AGENT', 'PLATFORM', 'notes:']

argparse tries to parse argv[1:], in this case a list of 10 strings. The ':' does not have any special meaning either to the shell (which splits up the commandline) or to the parser.

That list is what you'll have to work with if you parse it directly.

Comments

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.