Skip to content

No way to make the value of an option optional #69

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

Open
alerque opened this issue Sep 18, 2021 · 0 comments
Open

No way to make the value of an option optional #69

alerque opened this issue Sep 18, 2021 · 0 comments

Comments

@alerque
Copy link
Member

alerque commented Sep 18, 2021

In the 2.x series of releases it was possible to setup an option that had no value if not set, a default value if just the flag was added but no value given, and additionally a custom value if a value was passed. In 3.x this does not seem to be possible. Flags cannot take values and options always require a value. The default for options is only parsed in the event the option flag is not even passed.

As an example, I would like the value of a --foo=[VALUE] option to be optional with a default value if set, but allow the user to set (or not set) foo. If this is optopt.lua:

#!/usr/bin/env lua

local cli = require("cliargs")

cli:set_name("optopt")
cli:set_description("optional arg test")
cli:option("-f, --foo=[VALUE]", "foo val default to fiz", "fiz")
cli:flag("-h, --help", "display this help, then exit")

local opts, err = cli:parse(_G.arg)

if not opts and err then
  print(err)
  os.exit(1)
end

print("foo is", opts.foo)

What I get vs. what I expect:

# as expected
$ ./optopt.lua -f buz
foo is  buz

# unexpected, no option should mean no value
$ ./optopt.lua
foo is  fiz

# unexpected, setting the option should enable the default value if no value passed
$ ./optopt.lua -f
option -f requires a value to be set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant