-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Print default values even if non string #9
Comments
Hi @achalddave, sorry for such a late reply. Default values are always strings, if you pass a non-string it gets forwarded to |
This also affects whether a value is treated as being optional or not. I.e. take the following example: local argparse = require "argparse"
function open_if_string(filename)
if type(filename) == "string" then
return io.open(filename)
end
return filename
end
local parser = argparse()
parser:argument "input"
:description("Input file")
:args(1)
:default("default.file")
:convert(io.open)
parser:argument "output"
:description("Output file")
:args(1)
:default(io.stdout)
:convert(open_if_string)
local args = parser:parse() Calling this with
While using "STDOUT" as the default value and then converting that to the actual My proposal is to extend the parser:argument "input"
:description("Input file")
:args(1)
:default("default.file") -- just like above
:convert(io.open)
parser:argument "output"
:description("Output file")
:args(1)
:default(io.stdout, "STDOUT") -- with an optional 2nd argument
:convert(open_if_string) The output for
|
This gets even better: Apparently default values are completely ignored, if the argument is marked as optional with local argparse = require "argparse"
local parser = argparse()
parser:argument "input"
:description("Input file")
:args("?")
:default("default.file")
:convert(error)
local args = parser:parse()
print(args.input)
If I swap |
Typically a dash (
It can't always behave the same, it has to apply converter to string defaults but not to other types. What do you think about using
This is working as expected, if an argument is optional, there is no need to pass a default value to it. |
Thanks for the reminder. I'll do that for now.
Why does it have to behave in this way? Actually I was very surprised when I saw that it did not apply the converter to non-string values. I was under the assumption that it would always be my converter's task to convert the value into the form I desire, whether it originally was a string or not. So my first try after reading the documentation was simply to put the logic into the converter: If it is a string, assume it's a filename and open it; if it is already a file, just pass it on.
Is this documented somewhere? |
So the difference would be having Also, this is consistent with Python's argparse:
Looks like it isn't, I'll fix that. Currently the tutorial says:
But it doesn't say how many time it will be passed. And it will be passed minimum possible number of times, so, 0 with |
My idea was that this makes the library more flexible for different use-cases and developer expectations. (Because you give them exactly one thing and in all cases.)
Well, I'm also fine with that. Just make this behaviour very obvious in the documentation. And fix the Under these conditions I'd even go so far as to not support non-string values at all and just raise an error in this case. It would help with consistency and be much more obvious to the programmer with regards to what the expected input, output and behaviour are. I'm also fine with staying consistent with Python regarding non-string |
I'm not sure if I'm missing something in the documentation, but it seems that default values are not printed unless they are strings. I understand this may not be possible with complicated types, but with primitives such as doubles, this would be nice to have in the output of --help.
The text was updated successfully, but these errors were encountered: