3

Let's say that I want to enter info about multiple users from the command line AND that each user has multiple data associated with it.

Is it possible (syntactically permissible) to use argparse to parse something like myprpgrm.py --user john --email john@doe.com --valid Y --user jane --email jane@doe.com --valid N?

As you can see, each user should have 3 parameters associated with it - a name, an email and a valid flag.

Can argparse make sure that if a user parameter is seen, it must be accompanied by an email parameter and a validity parameter?

And can it give me the grouped information when I add multiple users, each with 3 parameters?


[Update] Could this be achieved somehow with Argument Groups?

7
  • why didn't you ask at Stack Overflow? meta.stackexchange.com/a/129632/165773 – gnat Jan 12 '15 at 13:12
  • Sorry, @GNAT, I just get so confused (and dismayed by the downvotes). I thought the decider was "if you are standing at a whiteboard, ask Programmers. If you are sitting at a keyboard ask S.O". I am asking here if this is even possible, so thought this to be the best place to ask. If I go to S.O they will jump all over me for not posting code (of something which may not be codeable). Please advise where I should post this in the S:E hierarchy - if at all. Thanks. – Mawg says reinstate Monica Jan 12 '15 at 13:43
  • I can't imagine one standing in front of whiteboard discussing concrete details of using argparse – gnat Jan 12 '15 at 13:50
  • 1
    Well, actually, the discussion is "how do we get these complicated parameters into the app? Can we use argparse? Is that even possible? Maybe we should use an INI file instead". We are not yet ready to discuss HOW to use argparse, because we don't even know yet if we CAN. As a generic help, can you state which S.E site is best suited for "can this be done"? type questions? Thanks. – Mawg says reinstate Monica Jan 12 '15 at 14:03
  • 1
    I would suggest phrasing your question less in terms of what is possible and more in terms of what is reasonable. Lots of things are possible. That doesn't mean they're maintainable, or good ideas in any way. – Kevin Jan 13 '15 at 3:07
2

I wouldn't use command line arguments for this. Command line arguments are meant for simple, transient configuration. What you have here is structured data. You want to put it into a more permanent format, such as JSON:

[
    {
        'user': 'john',
        'email': 'john@doe.com',
        'valid': true
    },
    {
        'user': 'jane',
        'email': 'jane@doe.com',
        'valid': false
    }
]

Python conveniently has a module for parsing JSON:

import json

with open('path/to/data.json') as file:
    data = json.load(file)

Argparse has special facilities for passing files on the command line; you probably won't have to call open() by yourself.

2
  • Thanks, @Kevin (+1). I am leaning towards .INI files and importing configparser, but JSON is also a good possibility. I will think which can best suit the sort of data which I am expecting. I do agree that it is probably too complex for a human to pass to the command line - I was originally thinking to add a GUI to invoke the command line based program, but am now leaning heavily towards having the data in a file. – Mawg says reinstate Monica Jan 13 '15 at 7:42
  • 2
    Three years later, I though that I would let you know that I did, after all, go with JSON. Thanks :-) – Mawg says reinstate Monica Apr 24 '18 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.