-
Notifications
You must be signed in to change notification settings - Fork 0
Implement initial CLI #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
81493b4
Add clap-based CLI with tests
leynos 6df4841
Add CLI error handling tests
leynos a22e20b
Refactor CLI parsing and tests
leynos b9d6cfd
Expand CLI behavioural coverage
leynos e6c33ea
Refine CLI parsing and tests
leynos f3a651e
Refactor apply_cli to parse arguments once
leynos f533a0d
Use expect for lint suppression
leynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| //! Command line interface definition using clap. | ||
| //! | ||
| //! This module defines the [`Cli`] structure and its subcommands. | ||
| //! It mirrors the design described in `docs/netsuke-design.md`. | ||
|
|
||
| use clap::{Parser, Subcommand}; | ||
| use std::path::PathBuf; | ||
|
|
||
| /// Maximum number of jobs accepted by the CLI. | ||
| const MAX_JOBS: usize = 64; | ||
|
|
||
| fn parse_jobs(s: &str) -> Result<usize, String> { | ||
| let value: usize = s | ||
| .parse() | ||
| .map_err(|_| format!("{s} is not a valid number"))?; | ||
| if (1..=MAX_JOBS).contains(&value) { | ||
| Ok(value) | ||
| } else { | ||
| Err(format!("jobs must be between 1 and {MAX_JOBS}")) | ||
| } | ||
| } | ||
|
|
||
| /// A modern, friendly build system that uses YAML and Jinja, powered by Ninja. | ||
| #[derive(Debug, Parser)] | ||
| #[command(author, version, about, long_about = None)] | ||
| pub struct Cli { | ||
| /// Path to the Netsuke manifest file to use. | ||
| #[arg(short, long, value_name = "FILE", default_value = "Netsukefile")] | ||
| pub file: PathBuf, | ||
|
|
||
| /// Change to this directory before doing anything. | ||
| #[arg(short = 'C', long, value_name = "DIR")] | ||
| pub directory: Option<PathBuf>, | ||
|
|
||
| /// Set the number of parallel build jobs. | ||
| #[arg(short, long, value_name = "N", value_parser = parse_jobs)] | ||
| pub jobs: Option<usize>, | ||
|
|
||
| #[command(subcommand)] | ||
| pub command: Option<Commands>, | ||
| } | ||
|
|
||
| impl Cli { | ||
| /// Parse command-line arguments, providing `build` as the default command. | ||
| #[must_use] | ||
| pub fn parse_with_default() -> Self { | ||
| Self::parse().with_default_command() | ||
| } | ||
|
|
||
| /// Parse the provided arguments, applying the default command when needed. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if argument parsing fails. | ||
| #[must_use] | ||
| pub fn parse_from_with_default<I, T>(args: I) -> Self | ||
| where | ||
| I: IntoIterator<Item = T>, | ||
| T: Into<std::ffi::OsString> + Clone, | ||
| { | ||
| Self::try_parse_from(args) | ||
| .unwrap_or_else(|e| panic!("CLI parsing failed: {e}")) | ||
| .with_default_command() | ||
| } | ||
|
|
||
| /// Apply the default command if none was specified. | ||
| #[must_use] | ||
| fn with_default_command(mut self) -> Self { | ||
| if self.command.is_none() { | ||
| self.command = Some(Commands::Build { | ||
| targets: Vec::new(), | ||
| }); | ||
| } | ||
| self | ||
| } | ||
| } | ||
|
|
||
| /// Available top-level commands for Netsuke. | ||
| #[derive(Debug, Subcommand, PartialEq, Eq, Clone)] | ||
| pub enum Commands { | ||
| /// Build specified targets (or default targets if none are given) [default]. | ||
| Build { | ||
| /// A list of specific targets to build. | ||
| targets: Vec<String>, | ||
| }, | ||
|
|
||
| /// Remove build artifacts and intermediate files. | ||
| Clean, | ||
|
|
||
| /// Display the build dependency graph in DOT format for visualization. | ||
| Graph, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| //! Netsuke core library. | ||
| //! | ||
| //! Currently this library only exposes the command line interface | ||
| //! definitions used by the binary and tests. | ||
|
|
||
| pub mod cli; | ||
| pub mod runner; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| use netsuke::{cli::Cli, runner}; | ||
|
|
||
| fn main() { | ||
| // Placeholder entry point for future CLI implementation. | ||
| let cli = Cli::parse_with_default(); | ||
| runner::run(cli); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| //! CLI execution and command dispatch logic. | ||
| //! | ||
| //! This module keeps [`main`] minimal by providing a single entry point that | ||
| //! handles command execution. It currently prints which command was invoked. | ||
|
|
||
| use crate::cli::{Cli, Commands}; | ||
|
|
||
| /// Execute the parsed [`Cli`] commands. | ||
| pub fn run(cli: Cli) { | ||
| match cli.command.unwrap_or(Commands::Build { | ||
| targets: Vec::new(), | ||
| }) { | ||
| Commands::Build { targets } => { | ||
| println!("Building targets: {targets:?}"); | ||
| } | ||
| Commands::Clean => { | ||
| println!("Clean requested"); | ||
| } | ||
| Commands::Graph => { | ||
| println!("Graph requested"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //! Unit tests for CLI argument parsing and validation. | ||
| //! | ||
| //! This module exercises the command-line interface defined in [`netsuke::cli`] | ||
| //! using `rstest` for parameterised coverage of success and error scenarios. | ||
| use clap::Parser; | ||
| use clap::error::ErrorKind; | ||
| use netsuke::cli::{Cli, Commands}; | ||
| use rstest::rstest; | ||
| use std::path::PathBuf; | ||
|
|
||
| #[rstest] | ||
| #[case(vec!["netsuke"], PathBuf::from("Netsukefile"), None, None, Commands::Build { targets: Vec::new() })] | ||
| #[case( | ||
| vec!["netsuke", "--file", "alt.yml", "-C", "work", "-j", "4", "build", "a", "b"], | ||
| PathBuf::from("alt.yml"), | ||
| Some(PathBuf::from("work")), | ||
| Some(4), | ||
| Commands::Build { targets: vec!["a".into(), "b".into()] }, | ||
| )] | ||
| fn parse_cli( | ||
|
sourcery-ai[bot] marked this conversation as resolved.
|
||
| #[case] argv: Vec<&str>, | ||
| #[case] file: PathBuf, | ||
| #[case] directory: Option<PathBuf>, | ||
| #[case] jobs: Option<usize>, | ||
| #[case] expected_cmd: Commands, | ||
| ) { | ||
| let cli = Cli::parse_from_with_default(argv.clone()); | ||
| assert_eq!(cli.file, file); | ||
| assert_eq!(cli.directory, directory); | ||
| assert_eq!(cli.jobs, jobs); | ||
| assert_eq!(cli.command.expect("command should be set"), expected_cmd); | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[case(vec!["netsuke", "unknowncmd"], ErrorKind::InvalidSubcommand)] | ||
| #[case(vec!["netsuke", "--file"], ErrorKind::InvalidValue)] | ||
| #[case(vec!["netsuke", "-j", "notanumber"], ErrorKind::ValueValidation)] | ||
| #[case(vec!["netsuke", "--file", "alt.yml", "-C"], ErrorKind::InvalidValue)] | ||
| fn parse_cli_errors(#[case] argv: Vec<&str>, #[case] expected_error: ErrorKind) { | ||
| let err = Cli::try_parse_from(argv).expect_err("unexpected success"); | ||
| assert_eq!(err.kind(), expected_error); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| use cucumber::World; | ||
|
leynos marked this conversation as resolved.
|
||
|
|
||
| #[derive(Debug, Default, World)] | ||
| pub struct CliWorld { | ||
| pub cli: Option<netsuke::cli::Cli>, | ||
| pub cli_error: Option<String>, | ||
| } | ||
|
|
||
| mod steps; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| CliWorld::run("tests/features").await; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| Feature: CLI parsing | ||
|
leynos marked this conversation as resolved.
|
||
|
|
||
|
leynos marked this conversation as resolved.
|
||
| Scenario: Build is the default command | ||
| When the CLI is parsed with "" | ||
| Then parsing succeeds | ||
| And the command is build | ||
|
|
||
| Scenario: Clean command runs | ||
| When the CLI is parsed with "-C work clean" | ||
| Then parsing succeeds | ||
| And the command is clean | ||
| And the working directory is "work" | ||
|
|
||
| Scenario: Graph command with jobs | ||
| When the CLI is parsed with "-j 2 graph" | ||
| Then parsing succeeds | ||
| And the command is graph | ||
| And the job count is 2 | ||
|
|
||
| Scenario: Manifest file can be overridden | ||
| When the CLI is parsed with "--file alt.yml build target" | ||
| Then parsing succeeds | ||
| And the manifest path is "alt.yml" | ||
| And the first target is "target" | ||
|
|
||
| Scenario: Unknown command fails | ||
| When the CLI is parsed with invalid arguments "unknown" | ||
| Then an error should be returned | ||
| And the error message should contain "unknown" | ||
|
|
||
| Scenario: Missing file argument value | ||
| When the CLI is parsed with invalid arguments "--file" | ||
| Then an error should be returned | ||
| And the error message should contain "--file" | ||
|
|
||
| Scenario: Directory flag sets working directory | ||
| When the CLI is parsed with "-C work build" | ||
| Then parsing succeeds | ||
| And the working directory is "work" | ||
|
|
||
| Scenario: Jobs flag sets parallelism | ||
| When the CLI is parsed with "-j 4" | ||
| Then parsing succeeds | ||
| And the job count is 4 | ||
|
|
||
| Scenario: Missing directory argument value | ||
| When the CLI is parsed with invalid arguments "-C" | ||
| Then an error should be returned | ||
| And the error message should contain "--directory" | ||
|
|
||
| Scenario: Missing jobs argument value | ||
| When the CLI is parsed with invalid arguments "-j" | ||
| Then an error should be returned | ||
| And the error message should contain "--jobs" | ||
|
|
||
| Scenario: Non-numeric jobs value | ||
| When the CLI is parsed with invalid arguments "-j notanumber" | ||
| Then an error should be returned | ||
| And the error message should contain "notanumber" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.