Subcommand new should not parse CommonArgs

This commit is contained in:
Jianfeng Jiang 2024-05-22 09:26:25 +00:00 committed by Tate, Hongliang Tian
parent 1391ff59f2
commit 3b95191f7e
3 changed files with 29 additions and 17 deletions

View File

@ -22,6 +22,9 @@ Use the kernel template.
If this option is not set, If this option is not set,
the library template will be used by default. the library template will be used by default.
`--library`:
Use the library template. This is the default option.
## Examples ## Examples
- Create a new kernel named `myos`: - Create a new kernel named `myos`:

View File

@ -18,32 +18,34 @@ use crate::{
}; };
pub fn main() { pub fn main() {
let (osdk_subcommand, common_args) = match Cli::parse() { let load_config = |common_args: &CommonArgs| {
Cli {
cargo_subcommand: CargoSubcommand::Osdk(osdk_subcommand),
common_args,
} => (osdk_subcommand, common_args),
};
let load_config = || {
let manifest = TomlManifest::load(); let manifest = TomlManifest::load();
let scheme = manifest.get_scheme(common_args.scheme.as_ref()); let scheme = manifest.get_scheme(common_args.scheme.as_ref());
Config::new(scheme, &common_args) Config::new(scheme, common_args)
}; };
match &osdk_subcommand { let cli = Cli::parse();
let CargoSubcommand::Osdk(osdk_subcommand) = &cli.cargo_subcommand;
match osdk_subcommand {
OsdkSubcommand::New(args) => execute_new_command(args), OsdkSubcommand::New(args) => execute_new_command(args),
OsdkSubcommand::Build(build_args) => { OsdkSubcommand::Build(build_args) => {
execute_build_command(&load_config(), build_args); execute_build_command(&load_config(&build_args.common_args), build_args);
} }
OsdkSubcommand::Run(run_args) => { OsdkSubcommand::Run(run_args) => {
execute_run_command(&load_config(), &run_args.gdb_server_args); execute_run_command(
&load_config(&run_args.common_args),
&run_args.gdb_server_args,
);
} }
OsdkSubcommand::Debug(debug_args) => { OsdkSubcommand::Debug(debug_args) => {
execute_debug_command(&load_config().run.build.profile, debug_args); execute_debug_command(
&load_config(&debug_args.common_args).run.build.profile,
debug_args,
);
} }
OsdkSubcommand::Test(test_args) => { OsdkSubcommand::Test(test_args) => {
execute_test_command(&load_config(), test_args); execute_test_command(&load_config(&test_args.common_args), test_args);
} }
OsdkSubcommand::Check(args) => execute_forwarded_command("check", &args.args), OsdkSubcommand::Check(args) => execute_forwarded_command("check", &args.args),
OsdkSubcommand::Clippy(args) => execute_forwarded_command("clippy", &args.args), OsdkSubcommand::Clippy(args) => execute_forwarded_command("clippy", &args.args),
@ -57,8 +59,6 @@ pub fn main() {
pub struct Cli { pub struct Cli {
#[clap(subcommand)] #[clap(subcommand)]
cargo_subcommand: CargoSubcommand, cargo_subcommand: CargoSubcommand,
#[command(flatten)]
common_args: CommonArgs,
} }
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
@ -154,12 +154,16 @@ pub struct BuildArgs {
value_name = "DIR" value_name = "DIR"
)] )]
pub output: Option<PathBuf>, pub output: Option<PathBuf>,
#[command(flatten)]
pub common_args: CommonArgs,
} }
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct RunArgs { pub struct RunArgs {
#[command(flatten)] #[command(flatten)]
pub gdb_server_args: GdbServerArgs, pub gdb_server_args: GdbServerArgs,
#[command(flatten)]
pub common_args: CommonArgs,
} }
#[derive(Debug, Args, Clone, Default)] #[derive(Debug, Args, Clone, Default)]
@ -197,6 +201,8 @@ pub struct DebugArgs {
default_value = ".aster-gdb-socket" default_value = ".aster-gdb-socket"
)] )]
pub remote: String, pub remote: String,
#[command(flatten)]
pub common_args: CommonArgs,
} }
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
@ -206,6 +212,8 @@ pub struct TestArgs {
help = "Only run tests containing this string in their names" help = "Only run tests containing this string in their names"
)] )]
pub test_name: Option<String>, pub test_name: Option<String>,
#[command(flatten)]
pub common_args: CommonArgs,
} }
#[derive(Debug, Args, Default, Clone, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Args, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
@ -255,6 +263,7 @@ impl CargoArgs {
} }
#[derive(Debug, Args)] #[derive(Debug, Args)]
/// Common args used for build, run, test and debug subcommand
pub struct CommonArgs { pub struct CommonArgs {
#[command(flatten)] #[command(flatten)]
pub build_args: CargoArgs, pub build_args: CargoArgs,

View File

@ -6,7 +6,7 @@ use crate::util::*;
fn cli_help_message() { fn cli_help_message() {
let output = cargo_osdk(&["-h"]).output().unwrap(); let output = cargo_osdk(&["-h"]).output().unwrap();
assert_success(&output); assert_success(&output);
assert_stdout_contains_msg(&output, "cargo osdk [OPTIONS] <COMMAND>"); assert_stdout_contains_msg(&output, "cargo osdk <COMMAND>");
} }
#[test] #[test]