sindresorhus/dargs — pull request #4 by radkodinev (2014-08-23 15:31:03+00:00)
Title: Support short options and handle some edge cases

@sindresorhus: Initially I added short options but a lot of cases with something going wrong came into my mind and I handled some of these as well in the same commit/PR. We may discuss those/such cases if you want and then I will split my PR accordingly if you want me to, and open issues and other PRs as well.

1. Add support for short options (`-a`, `-P 18`, `-6`, etc) using `dargs({ a: true, P: 18, 6: true })`.
Every single-character object key will be considered a short option if it is alphanumeric, or discarded otherwise, e.g. `'-': true` or `'@': 5` will be ignored.

1. Trim option names and disallow empty string option names, e.g.:
- `dargs({ num: 5, ' some ': 'value', abc: true })` would return `--num=5 -- some =value --abc` but now it correctly returns `--num=5 --some=value --abc` due to trimming.
- `dargs({ num: 5, ' some ': true, abc: true })` would return `--num=5 -- some --abc` but now it correctly returns `--num=5 --some --abc` due to trimming.
- `dargs({ num: 5, ' ': true, abc: true })`, with two or more spaces as option name, would return `--num=5 -- --abc` but now it correctly returns `--num=5 --abc` due to trimming and the empty string being discarded as an invalid option name.
- `dargs({ num: 5, ' ': 'value', abc: true })`, with two or more spaces as option name, would return `--num=5 -- =value --abc` but now it correctly returns `--num=5 --abc` due to trimming and the empty string being discarded as an invalid option name.

An empty string option name is possible in the following cases (with the 3rd one being most likely):
```
dargs({ num: 5, '': 'value' }); // or more spaces that will get trimmed to ''
```
```
options = { num: 5 };
options[''] = 'value'; // or more spaces that will get trimmed to ''
dargs(options);
```
```
options = { num: 5 };
options[getName()] = 'value'; // where getName returns a string that matches ^\s*$
dargs(options);
```

1. Add an option with a `NaN` value to tests.

__Other ideas__ (some or all may unnecessarily over-complicate or clutter dargs' code and maybe the user should be let do stupid things):
1. Limit option names to `\w`, e.g. 'x --' or 'x #' option names will cause damage :)
1. Trim option values.
1. Treat `-` (as in 'standard input') and `--` (as in 'treat all following arguments as non-option positional ones') specially, i.e. `'-': true` and `'--': true` which will output ` - ` and ` -- `.
1. Allow positional (non-option) arguments, i.e. currently there is no way to generate the following output: ` filename1 filename2`.