lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
386 stars 47 forks source link

Why positional arguments starts with "--" by default? #167

Closed szymonk211 closed 1 year ago

szymonk211 commented 1 year ago

When you see --argument in help you immediately think that this is an optional argument. I can't see why not remove "--" from every positional argument by default. It is natural, that positional arguments don't start with "--". This change probably wouldn't break executions of users scripts, because you don't have to write argument name in command line. It would not be an invasive change.

lebrice commented 1 year ago

Hey there @szymonk211 , thanks for posting!

I'm not sure if this answers your question, but you can create positional arguments explicitly by passing positional=True to the field function from simple-parsing. Here's an example of this.

I can't see why not remove "--" from every positional argument by default. It is natural, that positional arguments don't start with "--".

Hmm, I'm not sure I follow. Let's take an example, just to help me illustrate what I think you want.

# main.py
import simple_parsing
from dataclasses import dataclass

@dataclass
class Options:
    foo: int
    bar: float = 1.23

options = simple_parsing.parse(Options)
print(options)

Do you mean that the option string for foo should not start with "--"? or do you mean that the argument should be positional? I'm not sure I've ever seen a keyword argument without the leading "--", e.g. python main.py foo=123. Is that what you're suggesting?

If we were to make this argument positional (e.g. python main.py 123), then in which order would all the positional arguments from various dataclasses get assigned? To me, required keyword arguments are nicer in that case.

This change probably wouldn't break executions of users scripts, because you don't have to write argument name in command line.

Hmm, how would this be backward-compatible? Wouldn't their scripts break now that the --foo 123 flag doesn't work anymore?

szymonk211 commented 1 year ago

I'm sorry, my issue was a bit rambly. I just understood wrongly how the library works. I thought that arguments without the default value are positional, but they are optional with required keyword. The problem is that I wanted to use the library but i had no idea how to make positional arguments - which is a core feature of plain argument parser. Maybe some solution would be to give some example of positional argument in some visible place. You probably won't gonna change the core behaviour of the library so feel free to close the issue.

lebrice commented 1 year ago

By the way @szymonk211 , you can still just use regular argparse code with simple-parsing, since the simple_parsing.ArgumentParser is a subclass of argparse.ArgumentParser. So you could also just create the positional arguments with parser.add_argument("foo", type=int) or whatever you want. :)