diff --git a/src/command.rs b/src/command.rs index c467b2b..f38b087 100644 --- a/src/command.rs +++ b/src/command.rs @@ -14,7 +14,7 @@ pub fn define_command(options: Command) -> Command { #[napi] pub fn run(env: Env, cmd: Command, args: Option>) -> Result<()> { - let raw_args = resolve_option_args(args); + let raw_args = resolve_option_args(env, args)?; let clap = resolve_command(clap::Command::default(), Default::default(), &cmd); let matches = clap.clone().get_matches_from(&raw_args); diff --git a/src/resolver.rs b/src/resolver.rs index 0847360..a9ff856 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -5,10 +5,24 @@ use crate::{ utils::{leak_borrowed_str, leak_borrowed_str_or_default, leak_str}, }; -pub(crate) fn resolve_option_args(args: Option>) -> Vec { - let mut args = args.unwrap_or(std::env::args().collect()); +pub(crate) fn resolve_option_args( + env: napi::Env, + args: Option>, +) -> napi::Result> { + let mut args = match args { + Some(args) => args, + None => { + let mut args = std::env::args().collect::>(); + if args.is_empty() { + let process: napi::JsObject = env.get_global()?.get_named_property("process")?; + let argv: Vec = process.get_named_property("argv")?; + args = argv; + } + args + } + }; args.remove(0); // remove `node.exe` - args + Ok(args) } pub(crate) fn resolve_command_meta(