Argparse.add_argument() argument for additional functionallity

Hi. I would like to suggest to add functionality to argparse

For example we can have one ArgumentParser instance, for (windows, linux), (community edition, pro edition) apps. It could give ability to show and be able to run only allowed commands based on version of os, app or some other logic.

import argparse

def some_callable():
    if edition == 'pro_edition':
        return True
    return False

parser = argparse.ArgumentParser(
    prog="CustomProgramm", 
    description="CustomProgramms custom description",
    epilog='Epilog of this',
    )

parser.add_argument('--full-function', target=some_callable)

This --full-function argument will be available only if some_callable function is True

How’s this different from

if some_callable():
    parser.add_argument('--full-function')

?

2 Likes

@uranusjr thanks for replying! Technically, you can do how you want. No difference, you are right. But it’s not quite clean and easy as it could be implemented in argparse. target i think can be callable or some variable to check, whatever doesn’t matter, it can support both.

We have implemented a lot of tool, everytime for os detection or version detection we are using decorators or functions as you did. but i think it could be very easy and more understandable using the method i suggest.

It could be nice to get community suggestions about it.

I agree with @uranusjr, it seems like a pointless addition when you can do the same thing so easily already. I don’t find the suggestion cleaner as you suggest - on the contrary, I find it somewhat obscure and less understandable than an explicit condition. Sorry if that’s not what you’d hoped to hear…

3 Likes

@pf_moore thanks for replying. No it was just suggestion and wanted to learn opinion of experienced developers

1 Like