Class: ArgumentStream
Defined in: ArgumentStream.ts:5
Constructors
Constructor
new ArgumentStream(
results:ParserResult):ArgumentStream
Defined in: ArgumentStream.ts:9
Parameters
| Parameter | Type | 
|---|---|
| results | ParserResult | 
Returns
ArgumentStream
Properties
results
readonlyresults:ParserResult
Defined in: ArgumentStream.ts:6
state
state:
State
Defined in: ArgumentStream.ts:7
Accessors
finished
Get Signature
get finished():
boolean
Defined in: ArgumentStream.ts:17
Whether or not all ordered parameters were used.
Returns
boolean
length
Get Signature
get length():
number
Defined in: ArgumentStream.ts:24
The amount of ordered parameters.
Returns
number
remaining
Get Signature
get remaining():
number
Defined in: ArgumentStream.ts:31
The remaining amount of ordered parameters.
Returns
number
used
Get Signature
get used():
number
Defined in: ArgumentStream.ts:38
The amount of ordered parameters that have been used.
Returns
number
Methods
filter()
filter(
predicate: (value:string) =>boolean,from:number):Option<string[]>
Defined in: ArgumentStream.ts:497
Parameters
| Parameter | Type | 
|---|---|
| predicate | ( value:string) =>boolean | 
| from | number | 
Returns
Option<string[]>
filterAsync()
filterAsync(
predicate: (value:string) =>Promise<boolean>,from:number):Promise<Option<string[],boolean>>
Defined in: ArgumentStream.ts:514
Parameters
| Parameter | Type | 
|---|---|
| predicate | ( value:string) =>Promise<boolean> | 
| from | number | 
Returns
Promise<Option<string[], boolean>>
filterMap()
filterMap<
T>(predicate: (value:string) =>Option<T>,from:number):Option<T[]>
Defined in: ArgumentStream.ts:531
Type Parameters
| Type Parameter | 
|---|
| T | 
Parameters
| Parameter | Type | 
|---|---|
| predicate | ( value:string) =>Option<T> | 
| from | number | 
Returns
Option<T[]>
filterMapAsync()
filterMapAsync<
T>(predicate: (value:string) =>Promise<Option<T,boolean>>,from:number):Promise<Option<T[],boolean>>
Defined in: ArgumentStream.ts:549
Type Parameters
| Type Parameter | 
|---|
| T | 
Parameters
| Parameter | Type | 
|---|---|
| predicate | ( value:string) =>Promise<Option<T,boolean>> | 
| from | number | 
Returns
Promise<Option<T[], boolean>>
find()
find(
predicate: (value:string) =>boolean,from:number):Option<string>
Defined in: ArgumentStream.ts:248
Returns the value of the first element in the array within Option.some where predicate returns true, and
Option.none otherwise.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| predicate | ( value:string) =>boolean | find calls predicateonce for each unused ordered parameter, in ascending order, until it finds one wherepredicatereturnstrue. If such an element is found, find immediately returns aOption.somewith that element value. Otherwise, find returnsOption.none. | 
| from | number | The position where to start looking for unused parameters, defaults to current position. | 
Returns
Option<string>
The found parameter's value.
Note
This does not support asynchronous results, refer to findAsync.
Example
// Suppose args are from 'ba aa cc'.
console.log(args.find((value) => value.startsWith('a')));
// Some { value: 'aa' }
findAsync()
findAsync(
predicate: (value:string) =>Promise<boolean>,from:number):Promise<Option<string,boolean>>
Defined in: ArgumentStream.ts:282
Returns the value of the first element in the array within Option.some where predicate returns true, and
Option.none otherwise.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| predicate | ( value:string) =>Promise<boolean> | find calls predicateonce for each unused ordered parameter, in ascending order, until it finds one wherepredicatereturnstrue. If such an element is found, find immediately returns aOption.somewith that element value. Otherwise, find returnsOption.none. | 
| from | number | The position where to start looking for unused parameters, defaults to current position. | 
Returns
Promise<Option<string, boolean>>
The found parameter's value.
Note
This is an asynchronous variant of find.
Example
// Suppose args are from 'ba aa cc'.
console.log(args.find((value) => value.startsWith('a')));
// Some { value: 'aa' }
findMap()
findMap<
T>(predicate: (value:string) =>Option<T>,from:number):Option<T>
Defined in: ArgumentStream.ts:317
Returns the value of the first element in the array within Option.some where predicate returns Some, and
Option.none otherwise.
Type Parameters
| Type Parameter | 
|---|
| T | 
Parameters
| Parameter | Type | Description | 
|---|---|---|
| predicate | ( value:string) =>Option<T> | find calls predicateonce for each unused ordered parameter, in ascending order, until it finds one wherepredicatereturnsSome. If such an element is found, find immediately returns the returned value. Otherwise, find returnsOption.none. | 
| from | number | The position where to start looking for unused parameters, defaults to current position. | 
Returns
Option<T>
The found parameter's value.
Note
This does not support asynchronous results, refer to findMapAsync.
Example
// Suppose args are from 'ba aa cc'.
console.log(args.find((value) => value.startsWith('a')));
// Some { value: 'aa' }
Typeparam
T The output type.
findMapAsync()
findMapAsync<
T>(predicate: (value:string) =>Promise<Option<T,boolean>>,from:number):Promise<Option<T,boolean>>
Defined in: ArgumentStream.ts:353
Returns the value of the first element in the array within Option.some where predicate returns Some, and
Option.none otherwise.
Type Parameters
| Type Parameter | 
|---|
| T | 
Parameters
| Parameter | Type | Description | 
|---|---|---|
| predicate | ( value:string) =>Promise<Option<T,boolean>> | find calls predicateonce for each unused ordered parameter, in ascending order, until it finds one wherepredicatereturnsSome. If such an element is found, find immediately returns the returned value. Otherwise, find returnsOption.none. | 
| from | number | The position where to start looking for unused parameters, defaults to current position. | 
Returns
Promise<Option<T, boolean>>
The found parameter's value.
Note
This is an asynchronous variant of findMap.
Example
// Suppose args are from 'ba aa cc'.
console.log(args.find((value) => value.startsWith('a')));
// Some { value: 'aa' }
Typeparam
T The output type.
findParse()
findParse<
T,E>(predicate: (value:string) =>Result<T,E>,from:number):Result<T,E[]>
Defined in: ArgumentStream.ts:405
Finds and retrieves the first unused parameter that could be transformed.
Type Parameters
| Type Parameter | 
|---|
| T | 
| E | 
Parameters
| Parameter | Type | Description | 
|---|---|---|
| predicate | ( value:string) =>Result<T,E> | findParsecallspredicateonce for each unused ordered parameter, in ascending order, until it finds one wherepredicatereturnsOk. If such an element is found,findParseimmediately returns the returned value. Otherwise,findParsereturnsResult.Errwith all the returned errors. | 
| from | number | The position where to start looking for unused parameters, defaults to current position. | 
Returns
Result<T, E[]>
The found parameter's value.
Note
This is a variant of findMap that returns the errors on failure.
Note
This does not support asynchronous results, refer to findParseAsync.
Example
const parse = (value) => {
  const number = Number(value);
  return Number.isNaN(number)
    ? Result.err(`Could not parse ${value} to a number`)
    : Result.ok(number);
};
// Suppose args are from 'ba 1 cc'.
console.log(args.findParse(parse));
// Ok { value: 1 }
console.log(args.findParse(parse));
// Err {
//   error: [
//     'Could not parse ba to a number',
//     'Could not parse cc to a number'
//   ]
// }
Typeparam
T The output type.
Typeparam
E The error type.
findParseAsync()
findParseAsync<
T,E>(predicate: (value:string) =>Promise<Result<T,E,boolean>>,from:number):Promise<Result<T,E[],boolean>>
Defined in: ArgumentStream.ts:437
Finds and retrieves the first unused parameter that could be transformed.
Type Parameters
| Type Parameter | 
|---|
| T | 
| E | 
Parameters
| Parameter | Type | Description | 
|---|---|---|
| predicate | ( value:string) =>Promise<Result<T,E,boolean>> | findParsecallspredicateonce for each unused ordered parameter, in ascending order, until it finds one wherepredicatereturnsOk. If such an element is found,findParseimmediately returns the returned value. Otherwise,findParsereturnsResult.Errwith all the returned errors. | 
| from | number | The position where to start looking for unused parameters, defaults to current position. | 
Returns
Promise<Result<T, E[], boolean>>
The found parameter's value.
Note
This is a variant of findMapAsync that returns the errors on failure.
Note
This is an asynchronous variant of findParse.
Typeparam
T The output type.
Typeparam
E The error type.
flag()
flag(...
keys: readonlystring[]):boolean
Defined in: ArgumentStream.ts:587
Checks whether any of the flags were given.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| ... keys | readonly string[] | The names of the flags to check. | 
Returns
boolean
Whether or not any of the flags were given.
Example
// Assume args are '--f --g':
console.log(args.flag('f'));
// true
console.log(args.flag('g', 'h'));
// true
console.log(args.flag('h'));
// false
many()
many(
limit:number,from:number):Option<Parameter[]>
Defined in: ArgumentStream.ts:478
Retrieves multiple unused parameters.
Parameters
| Parameter | Type | Default value | Description | 
|---|---|---|---|
| limit | number | Infinity | The maximum amount of parameters to retrieve, defaults to Infinity. | 
| from | number | ... | The position where to start looking for unused parameters, defaults to current position. | 
Returns
Option<Parameter[]>
The unused parameters within the range.
Examples
// Assume args are '1 2 3':
console.log(join(args.many().unwrap()));
// '1 2 3'
// Assume args are '1 2 3':
console.log(join(args.many(2).unwrap()));
// '1 2'
option()
option(...
keys: readonlystring[]):Option<string>
Defined in: ArgumentStream.ts:610
Gets the last value of any option. When there are multiple names, the last value of the last found name is given.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| ... keys | readonly string[] | The names of the options to check. | 
Returns
Option<string>
The last value of the option, if any.
Example
// Assume args are '--a=1 --b=2 --c=3'.
console.log(args.option('a'));
// Some { value: '1' }
console.log(args.option('b', 'c'));
// Some { value: '3' }
console.log(args.option('d'));
// None {}
options()
options(...
keys: readonlystring[]):Option<readonlystring[]>
Defined in: ArgumentStream.ts:633
Gets all values from all options.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| ... keys | readonly string[] | The names of the options to check. | 
Returns
Option<readonly string[]>
The values from all the options concatenated, if any.
Example
// Assume args are '--a=1 --a=1 --b=2 --c=3'.
console.log(args.option('a'));
// Some { value: ['1', '1'] }
console.log(args.option('b', 'c'));
// Some { value: ['2', '3'] }
console.log(args.option('d'));
// None {}
reset()
reset():
void
Defined in: ArgumentStream.ts:654
Returns
void
restore()
restore(
state:State):void
Defined in: ArgumentStream.ts:650
Parameters
| Parameter | Type | 
|---|---|
| state | State | 
Returns
void
save()
save():
State
Defined in: ArgumentStream.ts:643
Returns
single()
single():
Option<string>
Defined in: ArgumentStream.ts:64
Retrieves the value of the next unused ordered token.
Returns
Option<string>
The value, if any.
Example
// Assume args are '1 2 3':
console.log(args.single());
// Ok { value: '1' }
console.log(args.single());
// Ok { value: '2' }
console.log(args.single());
// Ok { value: '3' }
console.log(args.single());
// None
singleMap()
singleMap<
T>(predicate: (value:string) =>Option<T>,useAnyways:boolean):Option<T>
Defined in: ArgumentStream.ts:107
Retrieves the value of the next unused ordered token, but only if it could be transformed.
Type Parameters
| Type Parameter | 
|---|
| T | 
Parameters
| Parameter | Type | Default value | Description | 
|---|---|---|---|
| predicate | ( value:string) =>Option<T> | undefined | The predicate that determines the parameter's mapped value, or nothing if failed. | 
| useAnyways | boolean | false | Whether to consider the parameter used even if the mapping failed. Defaults to false. | 
Returns
Option<T>
The mapped value, if any.
Note
This does not support asynchronous results, refer to singleMapAsync.
Example
const parse = (value) => {
  const number = Number(value);
  return Number.isNaN(number) ? Option.none : Option.some(number);
};
// Assume args are '1 2 3':
console.log(args.singleMap(parse));
// Some { value: 1 }
console.log(args.singleMap(parse));
// Some { value: 2 }
console.log(args.singleMap(parse));
// Some { value: 3 }
console.log(args.singleMap(parse));
// None
Typeparam
T The output type.
singleMapAsync()
singleMapAsync<
T>(predicate: (value:string) =>Promise<Option<T,boolean>>,useAnyways:boolean):Promise<Option<T,boolean>>
Defined in: ArgumentStream.ts:133
Retrieves the value of the next unused ordered token, but only if it could be transformed.
Type Parameters
| Type Parameter | 
|---|
| T | 
Parameters
| Parameter | Type | Default value | Description | 
|---|---|---|---|
| predicate | ( value:string) =>Promise<Option<T,boolean>> | undefined | The predicate that determines the parameter's mapped value, or nothing if failed. | 
| useAnyways | boolean | false | Whether to consider the parameter used even if the mapping failed. Defaults to false. | 
Returns
Promise<Option<T, boolean>>
The mapped value, if any.
Note
This is an asynchronous variant of singleMap.
Typeparam
T The output type.
singleParse()
singleParse<
T,E>(predicate: (value:string) =>Result<T,E>,useAnyways:boolean):Result<T,null|E>
Defined in: ArgumentStream.ts:185
Finds and retrieves the next unused parameter and transforms it.
Type Parameters
| Type Parameter | 
|---|
| T | 
| E | 
Parameters
| Parameter | Type | Default value | Description | 
|---|---|---|---|
| predicate | ( value:string) =>Result<T,E> | undefined | The predicate that determines the parameter's transformed value, or nothing if failed. | 
| useAnyways | boolean | false | Whether to consider the parameter used even if the transformation failed. Defaults to false. | 
Returns
Result<T, null | E>
The transformed value, if any.
Note
This is a variant of findMap that returns the errors on failure.
Note
This does not support asynchronous results, refer to singleParseAsync.
Example
const parse = (value) => {
  const number = Number(value);
  return Number.isNaN(number)
    ? Result.err(`Could not parse ${value} to a number`)
    : Result.ok(number);
};
// Assume args are '1 2 3':
console.log(args.singleParse(parse));
// Ok { value: 1 }
console.log(args.singleParse(parse));
// Ok { value: 2 }
console.log(args.singleParse(parse));
// Ok { value: 3 }
console.log(args.singleParse(parse));
// Err { error: null }
Typeparam
T The output type.
Typeparam
E The error type.
singleParseAsync()
singleParseAsync<
T,E>(predicate: (value:string) =>Promise<Result<T,E,boolean>>,useAnyways:boolean):Promise<Result<T,null|E,boolean>>
Defined in: ArgumentStream.ts:212
Retrieves the value of the next unused ordered token, but only if it could be transformed.
Type Parameters
| Type Parameter | 
|---|
| T | 
| E | 
Parameters
| Parameter | Type | Default value | Description | 
|---|---|---|---|
| predicate | ( value:string) =>Promise<Result<T,E,boolean>> | undefined | The predicate that determines the parameter's mapped value, or nothing if failed. | 
| useAnyways | boolean | false | Whether to consider the parameter used even if the mapping failed. Defaults to false. | 
Returns
Promise<Result<T, null | E, boolean>>
The mapped value, if any.
Note
This is an asynchronous variant of singleParse.
Typeparam
T The output type.
Typeparam
E The error type.