0

I am trying to find a way to write a command with options that can take additional options. The additional options can also apply to the command itself, but if expressed in a certain way, will be applied in a nested fashion.

For example, the syntax I am looking for would be something like the following (lines split for readability):

getFolks --name Bob --computers MacbookPro='123ABC' 
         Macbook ThinkPad='123EFG'[ --components 'ram>=10' ] 
         --components screenres='1440x900|1920x1080'

This is eventually going to be converted into a mysql query, and should be read like this:

"Get folks named Bob who have a ( MacbookPro with serial number 123ABC ), and a ( Macbook (any serial numbers ok) ), and a ( Thinkpad with serial number 123EFG with ram greater than or equal to 10 ) and all computers must have screen resolution of 1440x1260 or screen resolution of 1920x180.

I am trying to accomplish this without having to make "computers" become a sub command, since the --components option would be the same for both. Open to any suggestions however. Thanks for any ideas!

bwrabbit
  • 529
  • 1
  • 4
  • 25
  • If you want `--components` to apply to computers in general *and* a specific type of computer, then each computer would have to be a subcommand as well. `argparse` isn't the way to parse this kind of query language. – chepner Mar 06 '18 at 21:32
  • Can you rewrite that as a `usage` line? – hpaulj Mar 06 '18 at 21:45
  • @hpaulj do you mean write out the help string for the command? I can't really do that much better than I have explained in this question because I am not sure how it would work? I geuss I will just procede by parsing '--computer' option arguments using regex matches and look for square brackets and '--component' options, and then combine --computers and --components arguments into one big dictionary which I can then convert to an sql query. -- that is, if I can not figure out a better way to do some of this with argparse itself. – bwrabbit Mar 06 '18 at 21:51
  • How do you explain to your users the arguments, whether they are optional or not, how many values each, etc? The sort of thing that `-h` help produces. If you can't express the logic with a succinct usage line, you probably can't express it with `add_argument` commands. – hpaulj Mar 06 '18 at 22:01
  • This question and its links might help: https://stackoverflow.com/questions/49000217/cross-argument-validation-in-argparse – hpaulj Mar 06 '18 at 22:03

0 Answers0