@alcyone-labs/arg-parser 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +584 -0
- package/dist/ArgParser.d.ts +118 -0
- package/dist/ArgParser.d.ts.map +1 -0
- package/dist/FlagManager.d.ts +16 -0
- package/dist/FlagManager.d.ts.map +1 -0
- package/dist/index.cjs +1192 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.min.mjs +819 -0
- package/dist/index.min.mjs.map +1 -0
- package/dist/index.mjs +1192 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.d.ts +91 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +69 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { HandlerContext, IFlag, ISubCommand, ProcessedFlag, TParsedArgs } from "./types";
|
|
2
|
+
export declare class ArgParserError extends Error {
|
|
3
|
+
cmdChain: string[];
|
|
4
|
+
commandChain: string[];
|
|
5
|
+
constructor(message: string, cmdChain?: string[]);
|
|
6
|
+
}
|
|
7
|
+
interface IArgParserParams {
|
|
8
|
+
/**
|
|
9
|
+
* Add an extra new line between each flag group,
|
|
10
|
+
* makes the text more readable but uses more space
|
|
11
|
+
*
|
|
12
|
+
* Default: true
|
|
13
|
+
*/
|
|
14
|
+
extraNewLine?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Wraps the line at width, if shorter, wrapping will be more
|
|
17
|
+
* aggressive. Wrapping is based on words.
|
|
18
|
+
*
|
|
19
|
+
* Default: 50
|
|
20
|
+
* Minimum: 30
|
|
21
|
+
*/
|
|
22
|
+
wrapAtWidth?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Controls the placing of right text on the screen.
|
|
25
|
+
* The higher the value, the more to the right the text will be.
|
|
26
|
+
*
|
|
27
|
+
* Default: 30
|
|
28
|
+
* Minimum: 20
|
|
29
|
+
*/
|
|
30
|
+
blankSpaceWidth?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Character to display next to the flag to express mandatory fields.
|
|
33
|
+
*
|
|
34
|
+
* Default: *
|
|
35
|
+
*/
|
|
36
|
+
mandatoryCharacter?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Throw an error if a flag is added more than once
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
throwForDuplicateFlags?: boolean;
|
|
42
|
+
description?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Automatically handle ArgParserErrors by printing a formatted message
|
|
45
|
+
* and exiting. Set to false to catch ArgParserError manually.
|
|
46
|
+
* @default true
|
|
47
|
+
*/
|
|
48
|
+
handleErrors?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* The command name to display in help suggestions (e.g., 'dabl').
|
|
51
|
+
* If not provided, it falls back to appName or guessing from the script path.
|
|
52
|
+
* @since 1.5.1
|
|
53
|
+
*/
|
|
54
|
+
appCommandName?: string;
|
|
55
|
+
/**
|
|
56
|
+
* If true, when this parser is added as a sub-command, it will inherit
|
|
57
|
+
* flags from its direct parent *unless* a flag with the same name
|
|
58
|
+
* already exists in this parser. Child flags take precedence.
|
|
59
|
+
* @default false
|
|
60
|
+
*/
|
|
61
|
+
inheritParentFlags?: boolean;
|
|
62
|
+
}
|
|
63
|
+
interface IParseOptions {
|
|
64
|
+
/**
|
|
65
|
+
* When true, skips help flag processing (doesn't exit or show help)
|
|
66
|
+
* @default false
|
|
67
|
+
*/
|
|
68
|
+
skipHelpHandling?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* When true, skips the execution of any command handlers.
|
|
71
|
+
* @default false
|
|
72
|
+
*/
|
|
73
|
+
skipHandlers?: boolean;
|
|
74
|
+
}
|
|
75
|
+
type TParsedArgsWithRouting<T = any> = T & {
|
|
76
|
+
$commandChain?: string[];
|
|
77
|
+
handlerToExecute?: {
|
|
78
|
+
handler: Function;
|
|
79
|
+
context: HandlerContext;
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
export declare class ArgParser {
|
|
83
|
+
#private;
|
|
84
|
+
constructor(options?: IArgParserParams & {
|
|
85
|
+
appName?: string;
|
|
86
|
+
subCommands?: ISubCommand[];
|
|
87
|
+
handler?: (ctx: HandlerContext) => void;
|
|
88
|
+
}, initialFlags?: readonly IFlag[]);
|
|
89
|
+
get flags(): ProcessedFlag[];
|
|
90
|
+
get flagNames(): string[];
|
|
91
|
+
private _addToOutput;
|
|
92
|
+
addFlags(flags: readonly IFlag[]): this;
|
|
93
|
+
addFlag(flag: IFlag): this;
|
|
94
|
+
addSubCommand(subCommandConfig: ISubCommand): this;
|
|
95
|
+
/**
|
|
96
|
+
* Sets the handler function for this specific parser instance.
|
|
97
|
+
* This handler will be executed if this parser is the final one
|
|
98
|
+
* in the command chain and `executeHandlers` is enabled on the root parser.
|
|
99
|
+
*
|
|
100
|
+
* @param handler - The function to execute.
|
|
101
|
+
* @returns The ArgParser instance for chaining.
|
|
102
|
+
*/
|
|
103
|
+
setHandler(handler: (ctx: HandlerContext) => void): this;
|
|
104
|
+
printAll(filePath?: string): void;
|
|
105
|
+
parse(processArgs: string[], options?: IParseOptions): TParsedArgsWithRouting<any>;
|
|
106
|
+
/**
|
|
107
|
+
* Recursive helper for parsing arguments and handling sub-commands.
|
|
108
|
+
* This method assumes the global help check has already been performed in `parse`.
|
|
109
|
+
*/
|
|
110
|
+
private _parseRecursive;
|
|
111
|
+
helpText(): string;
|
|
112
|
+
getSubCommand(name: string): ISubCommand | undefined;
|
|
113
|
+
hasFlag(name: string): boolean;
|
|
114
|
+
getCommandChain(): string[];
|
|
115
|
+
getLastParseResult(): TParsedArgs<ProcessedFlag[]>;
|
|
116
|
+
}
|
|
117
|
+
export {};
|
|
118
|
+
//# sourceMappingURL=ArgParser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ArgParser.d.ts","sourceRoot":"","sources":["../src/ArgParser.ts"],"names":[],"mappings":"AAKA,O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E,CAAC,GAAG,EAAE,cAAc,KAAK,IAAI,CAAC;KACpC,EACN,YAAY,CAAC,EAAE,SAAS,KAAK,EAAE;IA6DjC,IAAI,KAAK,IAAI,aAAa,EAAE,CAE3B;IAED,IAAI,SAAS,IAAI,MAAM,EAAE,CAExB;IAED,OAAO,CAAC,YAAY;IAyDpB,QAAQ,CAAC,KAAK,EAAE,SAAS,KAAK,EAAE,GAAG,IAAI;IAKvC,OAAO,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI;IAK1B,aAAa,CAAC,gBAAgB,EAAE,WAAW,GAAG,IAAI;IAsClD;;;;;;;OAOG;IACH,UAAU,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,cAAc,KAAK,IAAI,GAAG,IAAI;IAKxD,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;IA8ajC,KAAK,CACH,WAAW,EAAE,MAAM,EAAE,EACrB,OAAO,CAAC,EAAE,aAAa,GACtB,sBAAsB,CAAC,GAAG,CAAC;IAsD9B;;;OAGG;IACH,OAAO,CAAC,eAAe;IAuLvB,QAAQ,IAAI,MAAM;IAgLX,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS;IAIpD,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAK9B,eAAe,IAAI,MAAM,EAAE;IAU3B,kBAAkB,IAAI,WAAW,CAAC,aAAa,EAAE,CAAC;CAqR1D"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IFlag, ProcessedFlag } from "./types";
|
|
2
|
+
export declare class FlagManager {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(options?: {
|
|
5
|
+
throwForDuplicateFlags?: boolean;
|
|
6
|
+
}, initialFlags?: readonly IFlag[]);
|
|
7
|
+
static _safeFlag(flag: IFlag): ProcessedFlag;
|
|
8
|
+
addFlag(flag: IFlag): this;
|
|
9
|
+
_setProcessedFlagForInheritance(processedFlag: ProcessedFlag): this;
|
|
10
|
+
addFlags(flags: readonly IFlag[]): this;
|
|
11
|
+
hasFlag(name: string): boolean;
|
|
12
|
+
getFlag(name: string): ProcessedFlag | undefined;
|
|
13
|
+
get flags(): ProcessedFlag[];
|
|
14
|
+
get flagNames(): string[];
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=FlagManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FlagManager.d.ts","sourceRoot":"","sources":["../src/FlagManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,aAAa,EAAiB,MAAM,SAAS,CAAC;AAE9D,qBAAa,WAAW;;gBAKpB,OAAO,GAAE;QAAE,sBAAsB,CAAC,EAAE,OAAO,CAAA;KAAO,EAClD,YAAY,GAAE,SAAS,KAAK,EAAO;IAMrC,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,GAAG,aAAa;IAwC5C,OAAO,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI;IAoB1B,+BAA+B,CAAC,aAAa,EAAE,aAAa,GAAG,IAAI;IAQnE,QAAQ,CAAC,KAAK,EAAE,SAAS,KAAK,EAAE,GAAG,IAAI;IAOvC,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI9B,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS;IAIhD,IAAI,KAAK,IAAI,aAAa,EAAE,CAE3B;IAED,IAAI,SAAS,IAAI,MAAM,EAAE,CAExB;CACF"}
|