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

refactor!: parsing, revisit short option groups, add support for combined short and value #75

Merged
merged 37 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2734d65
Support multiple unit test files
shadowspawn Mar 1, 2022
96f1447
Add isPossibleOptionValue
shadowspawn Mar 2, 2022
9dfd7b6
Add .editorconfig so editor knows about lint settings
shadowspawn Mar 2, 2022
638e07c
Add isLoneShortOption
shadowspawn Mar 2, 2022
c8c9e0c
Add isLongOption
shadowspawn Mar 2, 2022
04d4d95
Add separate dash tests
shadowspawn Mar 2, 2022
512afe5
Update signature for running new tests to arrow functions
shadowspawn Mar 2, 2022
83f0e02
isShortOptionGroup
shadowspawn Mar 2, 2022
686cbe3
Merge branch 'main' into feature/refactor-parse
shadowspawn Mar 2, 2022
bc45095
Update to new calling signature
shadowspawn Mar 3, 2022
0a9c04c
Add findLongOptionForShort
shadowspawn Mar 3, 2022
042d957
Start updating main parsing loop, and rework some utils.
shadowspawn Mar 3, 2022
8f85ecd
Switch loop to shift
shadowspawn Mar 3, 2022
cb93bfa
Add isShortOptionAndValue
shadowspawn Mar 3, 2022
4683053
Form expanded, clearer
shadowspawn Mar 3, 2022
aeff889
Fixes
shadowspawn Mar 3, 2022
0c399bc
Improve comments
shadowspawn Mar 3, 2022
db3e06e
New tests for short option group (and fixes)
shadowspawn Mar 3, 2022
3a7ea3c
Add tests for combining short and value
shadowspawn Mar 4, 2022
bb22e5a
Update package.json
shadowspawn Mar 4, 2022
eac5ecf
Update utils.js
shadowspawn Mar 4, 2022
419060c
Update utils.js
shadowspawn Mar 4, 2022
11bd06f
Add import
shadowspawn Mar 4, 2022
1c1d047
Add exports to keep utils private
shadowspawn Mar 4, 2022
3fcdcb3
AAA: Arrange, Act, Assert
shadowspawn Mar 4, 2022
cf48248
Add tests for failure duck typing
shadowspawn Mar 4, 2022
d2a1bc4
Add another dash example
shadowspawn Mar 4, 2022
22fd538
Make test for undefined more robust
shadowspawn Mar 4, 2022
a409102
Update utils.js
shadowspawn Mar 4, 2022
1bf4cfb
Update utils.js
shadowspawn Mar 5, 2022
228056b
Update utils.js
shadowspawn Mar 5, 2022
bc6dae7
Update package.json
shadowspawn Mar 5, 2022
6153fd2
Update index.js
shadowspawn Mar 5, 2022
eff783e
Comment improvements
shadowspawn Mar 5, 2022
90f9864
Update index.js
shadowspawn Mar 5, 2022
6013dc4
Update test/dash.js
shadowspawn Mar 5, 2022
e4b4f28
Expand test description per feedback
shadowspawn Mar 5, 2022
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
Prev Previous commit
Next Next commit
Add isLongOption
  • Loading branch information
shadowspawn committed Mar 2, 2022
commit c8c9e0c18a913b2065773ff9115924b637ff90dd
51 changes: 51 additions & 0 deletions test/is-long-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
/* eslint max-len: 0 */

const test = require('tape');
const { isLongOption } = require('../util.js');

test('isLongOption: when passed short option then returns false', function(t) {
t.false(isLongOption('-s'));
t.end();
});

test('isLongOption: when passed short option group then returns false', function(t) {
t.false(isLongOption('-abc'));
t.end();
});

test('isLongOption: when passed long option then returns true', function(t) {
t.true(isLongOption('--foo'));
t.end();
});

test('isLongOption: when passed long option with value then returns true', function(t) {
t.true(isLongOption('--foo=bar'));
t.end();
});

test('isLongOption: when passed empty string then returns false', function(t) {
t.false(isLongOption(''));
t.end();
});

test('isLongOption: when passed plain text then returns false', function(t) {
t.false(isLongOption('foo'));
t.end();
});

test('isLongOption: when passed single dash then returns false', function(t) {
t.false(isLongOption('-'));
t.end();
});

test('isLongOption: when passed double dash then returns false', function(t) {
t.false(isLongOption('--'));
t.end();
});

// This is a bit bogus, but simple consistent behaviour: long option follows double dash.
test('isLongOption: when passed arg starting with triple dash then returns true', function(t) {
t.true(isLongOption('---foo'));
t.end();
});
34 changes: 23 additions & 11 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const {
// and just tested implicitly via API).

/**
* Determines if the argument may be used as an option value.
* NB: We are choosing not to accept option-ish arguments.
* @example
* isPossibleOptionValue('V']) // returns true
* isPossibleOptionValue('-v') // returns false
* isPossibleOptionValue('--foo') // returns false
* isPossibleOptionValue(undefined) // returns false
*/
* Determines if the argument may be used as an option value.
* NB: We are choosing not to accept option-ish arguments.
* @example
* isPossibleOptionValue('V']) // returns true
* isPossibleOptionValue('-v') // returns false
* isPossibleOptionValue('--foo') // returns false
* isPossibleOptionValue(undefined) // returns false
*/
function isPossibleOptionValue(value) {
if (value === undefined) return false;
if (value === '-') return true; // e.g. representing stdin/stdout for file
Expand All @@ -32,16 +32,28 @@ function isPossibleOptionValue(value) {
}

/**
* Determines if `arg` is a just a short option.
* @example '-f'
*/
* Determines if `arg` is a just a short option.
* @example '-f'
*/
function isLoneShortOption(arg) {
return arg.length === 2 &&
StringPrototypeCharAt(arg, 0) === '-' &&
StringPrototypeCharAt(arg, 1) !== '-';
}

/**
* Determines if `arg` is a long option, which may have a trailing value.
* @example
* isLongOption('-a) // returns false
* isLongOption('--foo) // returns true
* isLongOption('--foo=bar) // returns true
*/
function isLongOption(arg) {
return arg.length > 2 && StringPrototypeStartsWith(arg, '--');
}

module.exports = {
isLongOption,
isLoneShortOption,
isPossibleOptionValue
};