Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic support for shorts #50

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'] };
shadowspawn marked this conversation as resolved.
Show resolved Hide resolved
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