You can't do what you're asking for with yargs, and arguably you shouldn't. It's easy to see why you can't by looking at what yargs-parser (the module yargs uses to parse your arguments) returns for the different argument styles you mentioned:
console.log(yargsParser(['-m', 'A']));
console.log(yargsParser(['-m2', 'B']));
console.log(yargsParser(['--m2', 'C']));
<script src="https://bundle.run/yargs-parser@11.1.1"></script>
As you can see:
-m A
is parsed as option m
with value A
.
-m2 B
is parsed as option m
with value 2
and, separately, array parameter B
.
--m2 C
is parsed as option m2
with value C
.
There is not a way to make the parser behave differently. This is with good reason: Most command line tools (Windows notwithstanding) behave this way.
By decades-old convention, "long" options use two dashes followed by a human-readable name, like --max-count
. "Short" options, which are often aliases for long ones, use a single dash followed by a single character, like -m
, and—this is the crucial bit—if the option takes a value, the space between the option and the value can be omitted. This is why when yargs sees -m2
, it assumes 2
is the value for the m
option.
If you want to use yargs (and not confuse command line-savvy users) you'll you need to change your options to either 1. --m
and --m2
or, 2. -m
and (for example) -n
.