0

I think my problem is very similar to this question, but it's not exact, and unfortunately I haven't been able to extrapolate my solution.

I have some arguments that I only want to use if another argument is set to 'False'. Otherwise, these arguments should not be required, and, in fact, should not be provided.

Presently, my code basically looks like this:

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    #Arguments that will always be mandatory
    #str2bool is a function I have to detect T/F, which I've verified works
    parser.add_argument('--null_variants_provided', type=str2bool, nargs='?',
                        help='Are you providing a list of null variants?
                              If not, more arguments will be required',
                              required=True)
    args = parser.parse_args()



    #Options that will sometimes be necessary
    if args.null_variants_provided is False:
        parser.add_argument('--LCR_regions_file', 
                             help='I need to be required if
                             above argument is False', required=True)
        args = parser.parse_args()

The reason the solution I linked to won't work for me, I suspect, is it's simply checking if a particular argument was passed- it's not checking what that argument is. The reason my solution above does not work, I suspect, is because once I set args the first time, the code checks which arguments have been passed, and if there are any extra arguments (namely, arguments that I define later), it throws an error.

Can you guys think of a way to do this?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
John Rouhana
  • 538
  • 3
  • 15
  • Why not just accept both arguments, and check the values after parsing? – hpaulj Oct 25 '18 at 19:26
  • Hi hpaulj, Thanks for the suggestion. The reason is because I'm hoping to make an automatic logger that logs what arguments were run each time this program was used. While your solution would work, I'd be left with a log file that's more messy than it needs to be, which I'm hoping to avoid. – John Rouhana Oct 25 '18 at 19:28
  • Why `nargs='?' and `required=True`. The '?' is most valuable when you provide both a 'default' and 'const'. Then you get a 3 way switch. – hpaulj Oct 25 '18 at 19:29
  • A more recent question along this line: https://stackoverflow.com/questions/52946555/python-argparse-if-argument-1-not-present-how-to-continue-and-set-required-nex, though my suggestion is the same. – hpaulj Oct 25 '18 at 19:31
  • I don't follow what a logger has to do with this. 'what arguments were run' is a rather vague description. `argparse` doesn't implement any direct logging actions, so it doesn't matter if you do argument testing or whether the parser does it. – hpaulj Oct 25 '18 at 19:34
  • Thanks hpaulj. If it comes down to it, I'll go with that. The nargs='?' is a mistake- you're right about that. When I say 'what arguments were run', I mean that I more or less just want to capture the entire user input for each run, annotate it with an output folder, and save that. That way specific arguments can be linked to specific output content. For example, I might get different results if I decide to filter out certain regions one time, and not to another. Capturing user input is far less messy if only minimal necessary user inputs are recorded. – John Rouhana Oct 25 '18 at 19:34

0 Answers0