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?

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

3 Likes

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.

1 Like