2

How would I be able to set up my argparser to have the following behaviour?

backup -c example
backup example
backup -r example
backup -l

backup --create example2
backup example2
backup --remove example2
backup --list

I would like to have my program have an assortment of flags to create/list/delete 'processes' not being outlined here. My intention that processes can be created with the --create flag, which can then be called by using the name provided. The process could be deleted by passing the name to the --delete flag. I would like to be able to have the non-flag argument (the name of the process that is to be run) passable without conflicting with the other flags which may or may not be store_true (in the case that the action is to list) or an argument (such as create/delete)

If I use a positional argument to capture the name of the backup process then I loose the ability to pass the flags in their intended manner the positional argument is required. However, without one I can't pass unspecified (arguments without a flag) arguments.

In this example you can imagine that -c creates an artefact with the given name, calling that name outright triggers some computation to occur, and -r removes the artefact with the given name.

hi im Bacon
  • 374
  • 1
  • 12
  • 1
    What are `-c` and `-r` for? You need to be more clear by giving some run examples so we can help you. Because right now it seems like `example` should be positional and `-c` and `-r` optional and that's it... – Tomerikoo Dec 21 '19 at 14:03
  • Why can't the middle case be flagged? What's special or different about it? What `usage` do you want to see? – hpaulj Dec 21 '19 at 16:32
  • @Tomerikoo You are right! this is a duplicate, just need to create a position argument with nargs='?'. Thanks for your help! – hi im Bacon Dec 21 '19 at 18:13
  • @hiimBacon Why would it be optional? Your examples imply that the `example` argument is required (and that `example` is required verbatim, but that doesn't make much sense). In any case I'm voting to close the question as unclear, but I'm happy that you've found a solution. If you want to keep the question open, you can [edit] it to clarify. – wjandrea Dec 22 '19 at 17:58
  • Oh, I see, you're not aware that `action='store_true'` exists. See @skillsmuggler's answer. This is where a [mre] would really help. – wjandrea Dec 22 '19 at 18:02
  • Does this answer your question? [Argparse optional positional arguments?](https://stackoverflow.com/questions/4480075/argparse-optional-positional-arguments) – Stas Buzuluk Dec 28 '19 at 14:35
  • Yeah, my bad, I'll make sure to expand the example next time. I naively thought that the answer would jump right out and that the implementation/expected use wouldn't have mattered, but I can see how its hard when what is being asked isn't clear. Thanks for your help. – hi im Bacon Dec 28 '19 at 14:40

1 Answers1

2

According to my understanding, you want to make use of arguments as flags for you program.

# Creating flag based argument
# Set to true if "--c" is mentioned
parser.add_argument('--c', action='store_true', help='create')

# Parsing
opt = parser.parse_args()

# Using argument
if opt.c:
    create()
skillsmuggler
  • 1,862
  • 1
  • 11
  • 16
  • Nice! Though it'd probably be better to use GNU-style options, e.g. `'-c', '--create'`, then later `opt.create`. Also BTW If OP wants to prevent creation and removal at the same time, they could use a [mutually exclusive group](https://docs.python.org/3/library/argparse.html#mutual-exclusion). – wjandrea Dec 22 '19 at 18:07