Skip to content

Commit

Permalink
feat: basic support for shorts (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Feb 4, 2022
1 parent 724df10 commit a2f36d7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,20 @@ const parseArgs = (
ArrayPrototypeSlice(argv, ++pos)
);
return result;
} else if (
StringPrototypeCharAt(arg, 1) !== '-'
) { // Look for shortcodes: -fXzy
throw new ERR_NOT_IMPLEMENTED('shortcodes');
} else if (StringPrototypeCharAt(arg, 1) !== '-') {
// Look for shortcodes: -fXzy
if (arg.length > 2) {
throw new ERR_NOT_IMPLEMENTED('short option groups');
}

arg = StringPrototypeCharAt(arg, 1); // short
if (options.short && options.short[arg])
arg = options.short[arg]; // now long!
// ToDo: later code tests for `=` in arg and wrong for shorts
} else {
arg = StringPrototypeSlice(arg, 2); // remove leading --
}

arg = StringPrototypeSlice(arg, 2); // remove leading --

if (StringPrototypeIncludes(arg, '=')) {
// Store option=value same way independent of `withValue` as:
// - looks like a value, store as a value
Expand Down
64 changes: 64 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,70 @@ const { parseArgs } = require('../index.js');

// Test results are as we expect

test('when short option used as flag then stored as flag', function(t) {
const passedArgs = ['-f'];
const expected = { flags: { f: true }, values: { f: undefined }, positionals: [] };
const args = parseArgs(passedArgs);

t.deepEqual(args, expected);

t.end();
});

test('when short option used as flag before positional then stored as flag and positional (and not value)', function(t) {
const passedArgs = ['-f', 'bar'];
const expected = { flags: { f: true }, values: { f: undefined }, positionals: [ 'bar' ] };
const args = parseArgs(passedArgs);

t.deepEqual(args, expected);

t.end();
});

test('when short option withValue used with value then stored as value', function(t) {
const passedArgs = ['-f', 'bar'];
const passedOptions = { withValue: ['f'] };
const expected = { flags: { f: true }, values: { f: 'bar' }, positionals: [] };
const args = parseArgs(passedArgs, passedOptions);

t.deepEqual(args, expected);

t.end();
});

test('when short option listed in short used as flag then long option stored as flag', function(t) {
const passedArgs = ['-f'];
const passedOptions = { short: { f: 'foo' } };
const expected = { flags: { foo: true }, values: { foo: undefined }, positionals: [] };
const args = parseArgs(passedArgs, passedOptions);

t.deepEqual(args, expected);

t.end();
});

test('when short option listed in short and long listed in withValue and used with value then long option stored as value', function(t) {
const passedArgs = ['-f', 'bar'];
const passedOptions = { short: { f: 'foo' }, withValue: ['foo'] };
const expected = { flags: { foo: true }, values: { foo: 'bar' }, positionals: [] };
const args = parseArgs(passedArgs, passedOptions);

t.deepEqual(args, expected);

t.end();
});

test('when short option withValue used without value then stored as flag', function(t) {
const passedArgs = ['-f'];
const passedOptions = { withValue: ['f'] };
const expected = { flags: { f: true }, values: { f: undefined }, positionals: [] };
const args = parseArgs(passedArgs, passedOptions);

t.deepEqual(args, expected);

t.end();
});

test('Everything after a bare `--` is considered a positional argument', function(t) {
const passedArgs = ['--', 'barepositionals', 'mopositionals'];
const expected = { flags: {}, values: {}, positionals: ['barepositionals', 'mopositionals'] };
Expand Down

0 comments on commit a2f36d7

Please sign in to comment.