Phillipp

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

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

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

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.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement