Phillipp
OptionParser alias behavior
Hey,
I am building a CLI tool and use the OptionParser module to parse the arguments.
Now I got a --config-override option which accepts a string. I also wanted to have a -co short version (alias). So this was my initial configuration:
{switches, _, _} = OptionParser.parse(
args,
switches: [
config_override: :string
],
aliases: [
co: :config_override
]
)
But then, at runtime, I got a warning:
warning: multi-letter aliases are deprecated, got: :co
Fine, I then changed the alias to just -o:
{switches, _, _} = OptionParser.parse(
args,
switches: [
config_override: :string
],
aliases: [
o: :config_override
]
)
The weird behavior I experience is, that not only -o works but also -co. Where does the -co come from?
Marked As Solved
amarraja
I’m not familiar with the option parser module, however in shell convention -co is shorthand for -c -o - that is why they can only be single letters.
In your case, -o is activating the switch, and -c is probably just getting ignored
Also Liked
Phillipp
Ah, that might explain it. So the module seems to implement that convention as well.
iex(48)> args = ["-co", "foo"]
["-co", "foo"]
iex(49)> {switches, _, _} = OptionParser.parse(
...(49)> args,
...(49)> switches: [
...(49)> debug: :boolean,
...(49)> config_override: :string
...(49)> ],
...(49)> aliases: [
...(49)> o: :config_override
...(49)> ]
...(49)> )
{[config_override: "foo"], [], [{"-c", nil}]}
As you said, -c is ignored.
Last Post!
Phillipp
Ah, that might explain it. So the module seems to implement that convention as well.
iex(48)> args = ["-co", "foo"]
["-co", "foo"]
iex(49)> {switches, _, _} = OptionParser.parse(
...(49)> args,
...(49)> switches: [
...(49)> debug: :boolean,
...(49)> config_override: :string
...(49)> ],
...(49)> aliases: [
...(49)> o: :config_override
...(49)> ]
...(49)> )
{[config_override: "foo"], [], [{"-c", nil}]}
As you said, -c is ignored.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










