diff --git a/crates/nargo_cli/src/cli/init_cmd.rs b/crates/nargo_cli/src/cli/init_cmd.rs index 576690b7fab..a1d4576758a 100644 --- a/crates/nargo_cli/src/cli/init_cmd.rs +++ b/crates/nargo_cli/src/cli/init_cmd.rs @@ -9,7 +9,11 @@ use std::path::PathBuf; /// Create a Noir project in the current directory. #[derive(Debug, Clone, Args)] -pub(crate) struct InitCommand; +pub(crate) struct InitCommand { + /// Name of the package [default: current directory name] + #[clap(long)] + name: Option, +} const EXAMPLE: &str = r#"fn main(x : Field, y : pub Field) { assert(x != y); @@ -27,17 +31,20 @@ fn test_main() { pub(crate) fn run( // Backend is currently unused, but we might want to use it to inform the "new" template in the future _backend: &B, - _args: InitCommand, + args: InitCommand, config: NargoConfig, ) -> Result<(), CliError> { - initialize_project(config.program_dir); + let package_name = args + .name + .unwrap_or_else(|| config.program_dir.file_name().unwrap().to_str().unwrap().to_owned()); + + initialize_project(config.program_dir, &package_name); Ok(()) } /// Initializes a new Noir project in `package_dir`. -pub(crate) fn initialize_project(package_dir: PathBuf) { +pub(crate) fn initialize_project(package_dir: PathBuf, package_name: &str) { // TODO: Should this reject if we have non-Unicode filepaths? - let package_name = package_dir.file_name().expect("Expected a filename").to_string_lossy(); let src_dir = package_dir.join(SRC_DIR); create_named_dir(&src_dir, "src"); diff --git a/crates/nargo_cli/src/cli/new_cmd.rs b/crates/nargo_cli/src/cli/new_cmd.rs index 66c29eb3292..a792e752a51 100644 --- a/crates/nargo_cli/src/cli/new_cmd.rs +++ b/crates/nargo_cli/src/cli/new_cmd.rs @@ -8,10 +8,12 @@ use std::path::PathBuf; /// Create a Noir project in a new directory. #[derive(Debug, Clone, Args)] pub(crate) struct NewCommand { - /// Name of the package - package_name: String, /// The path to save the new project - path: Option, + path: PathBuf, + + /// Name of the package [default: package directory name] + #[clap(long)] + name: Option, } pub(crate) fn run( @@ -20,12 +22,14 @@ pub(crate) fn run( args: NewCommand, config: NargoConfig, ) -> Result<(), CliError> { - let package_dir = config.program_dir.join(args.package_name); + let package_dir = config.program_dir.join(&args.path); if package_dir.exists() { return Err(CliError::DestinationAlreadyExists(package_dir)); } - initialize_project(package_dir); + let package_name = + args.name.unwrap_or_else(|| args.path.file_name().unwrap().to_str().unwrap().to_owned()); + initialize_project(package_dir, &package_name); Ok(()) }