Skip to content

Commit 8f34ec5

Browse files
authored
feat: support pass help option for args (#41)
* feat: support pass help option for args * chore: code lint
1 parent d4f5645 commit 8f34ec5

File tree

6 files changed

+53
-1
lines changed

6 files changed

+53
-1
lines changed

.changeset/selfish-avocados-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'archons': patch
3+
---
4+
5+
Support pass help option for args

examples/simple.cts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const main = defineCommand({
2828
verbose: {
2929
type: 'flag',
3030
parser: 'boolean',
31+
help: 'Enable verbose output',
3132
},
3233
},
3334
subcommands: {

index.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,25 @@
66
export declare function defineCommand(options: Command): Command
77
export declare function run(cmd: Command, args?: Array<string> | undefined | null): void
88
export const VERSION: string
9+
/**
10+
* Command context
11+
*
12+
* This is the context object that is passed to the command callback.
13+
*/
914
export interface Context {
15+
/**
16+
* Parsed arguments
17+
*
18+
* This is a js object that contains the parsed arguments.
19+
* The keys of the object are the names of the arguments and
20+
* the values are the parsed values.
21+
*/
1022
args: object
23+
/**
24+
* Raw arguments
25+
*
26+
* The raw arguments parsed by command line or manually given.
27+
*/
1128
rawArgs: Array<string>
1229
}
1330
/** Command metadata */
@@ -57,10 +74,17 @@ export interface CommandOption {
5774
long?: string
5875
alias?: Array<string>
5976
hiddenAlias?: Array<string>
77+
help?: string
6078
required?: boolean
6179
default?: string
6280
hidden?: boolean
6381
}
82+
/**
83+
* Command definition
84+
*
85+
* This is the object that defines a command.
86+
* It contains the metadata, options, and callback function.
87+
*/
6488
export interface Command {
6589
meta: CommandMeta
6690
options: Record<string, CommandOption>

src/command.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ pub fn run(env: Env, cmd: Command, args: Option<Vec<String>>) -> Result<()> {
3737
if let Some(cb) = cmd.callback.as_ref() {
3838
cb.call1::<Context, JsNull>(context)?;
3939
} else {
40-
env.throw_error("No callback function found for command", None)?;
40+
env.throw_error(
41+
"No callback function found for main command and no subcommand was provided.",
42+
Some("E_NO_CALLBACK"),
43+
)?;
4144
};
4245
}
4346
Ok(())

src/types.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ use napi_derive::napi;
66
#[napi]
77
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
88

9+
/// Command context
10+
///
11+
/// This is the context object that is passed to the command callback.
912
#[napi(object)]
1013
pub struct Context {
14+
/// Parsed arguments
15+
///
16+
/// This is a js object that contains the parsed arguments.
17+
/// The keys of the object are the names of the arguments and
18+
/// the values are the parsed values.
1119
pub args: JsObject,
20+
/// Raw arguments
21+
///
22+
/// The raw arguments parsed by command line or manually given.
1223
pub raw_args: Vec<String>,
1324
}
1425

@@ -58,11 +69,16 @@ pub struct CommandOption {
5869
pub long: Option<String>,
5970
pub alias: Option<Vec<String>>,
6071
pub hidden_alias: Option<Vec<String>>,
72+
pub help: Option<String>,
6173
pub required: Option<bool>,
6274
pub default: Option<String>,
6375
pub hidden: Option<bool>,
6476
}
6577

78+
/// Command definition
79+
///
80+
/// This is the object that defines a command.
81+
/// It contains the metadata, options, and callback function.
6682
#[napi(object)]
6783
pub struct Command {
6884
pub meta: CommandMeta,

src/utils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ pub(crate) fn resolve_command_options(
101101
.collect::<Vec<&str>>();
102102
arg = arg.aliases(hidden_alias);
103103
}
104+
if let Some(help) = &opt.help {
105+
arg = arg.help(leak_borrowed_str(help));
106+
}
104107
if let Some(required) = opt.required {
105108
arg = arg.required(required);
106109
}

0 commit comments

Comments
 (0)