mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
types/argparse: Minimal support for custom actions
This commit is contained in:
parent
9b7ed4b3ec
commit
ffed741a00
@ -1,6 +1,12 @@
|
||||
// near copy of each of the tests from https://github.com/nodeca/argparse/tree/master/examples
|
||||
|
||||
import { ArgumentParser, RawDescriptionHelpFormatter } from 'argparse';
|
||||
import {
|
||||
ArgumentParser,
|
||||
RawDescriptionHelpFormatter,
|
||||
Action,
|
||||
ActionConstructorOptions,
|
||||
Namespace,
|
||||
} from 'argparse';
|
||||
let args: any;
|
||||
|
||||
const simpleExample = new ArgumentParser({
|
||||
@ -276,3 +282,26 @@ group.addArgument(['--bar'], {
|
||||
help: 'bar help'
|
||||
});
|
||||
formatterExample.printHelp();
|
||||
|
||||
class CustomAction1 extends Action {
|
||||
constructor(options: ActionConstructorOptions) {
|
||||
super(options);
|
||||
}
|
||||
call(parser: ArgumentParser, namespace: Namespace, values: string | string[], optionString: string | null) {
|
||||
console.log('custom action 1');
|
||||
}
|
||||
}
|
||||
|
||||
class CustomAction2 extends Action {
|
||||
call(parser: ArgumentParser, namespace: Namespace, values: string | string[], optionString: string | null) {
|
||||
console.log('custom action 2');
|
||||
}
|
||||
}
|
||||
|
||||
const customActionExample = new ArgumentParser({ addHelp: false });
|
||||
customActionExample.addArgument('--abc', {
|
||||
action: CustomAction1,
|
||||
});
|
||||
customActionExample.addArgument('--def', {
|
||||
action: CustomAction2,
|
||||
});
|
||||
|
||||
14
types/argparse/index.d.ts
vendored
14
types/argparse/index.d.ts
vendored
@ -3,6 +3,7 @@
|
||||
// Definitions by: Andrew Schurman <https://github.com/arcticwaters>
|
||||
// Tomasz Łaziuk <https://github.com/tlaziuk>
|
||||
// Sebastian Silbermann <https://github.com/eps1lon>
|
||||
// Kannan Goundan <https://github.com/cakoose>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
@ -79,13 +80,24 @@ export interface ArgumentGroupOptions {
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export abstract class Action {
|
||||
protected dest: string;
|
||||
constructor(options: ActionConstructorOptions);
|
||||
abstract call(parser: ArgumentParser, namespace: Namespace, values: string | string[], optionString: string | null): void;
|
||||
}
|
||||
|
||||
// Passed to the Action constructor. Subclasses are just expected to relay this to
|
||||
// the super() constructor, so using an "opaque type" pattern is probably fine.
|
||||
// Someone may want to fill this out in the future.
|
||||
export type ActionConstructorOptions = number & {_: 'ActionConstructorOptions'};
|
||||
|
||||
export class HelpFormatter { }
|
||||
export class ArgumentDefaultsHelpFormatter { }
|
||||
export class RawDescriptionHelpFormatter { }
|
||||
export class RawTextHelpFormatter { }
|
||||
|
||||
export interface ArgumentOptions {
|
||||
action?: string;
|
||||
action?: string | { new(options: ActionConstructorOptions): Action };
|
||||
optionStrings?: string[];
|
||||
dest?: string;
|
||||
nargs?: string | number;
|
||||
|
||||
@ -21,4 +21,4 @@
|
||||
"index.d.ts",
|
||||
"argparse-tests.ts"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user