Skip to content

Commit

Permalink
feat(cli): Split CLI into function for subcommands and validate args
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Apr 24, 2020
1 parent 6d5f6ce commit 1f46ea2
Showing 1 changed file with 62 additions and 6 deletions.
68 changes: 62 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use structopt::StructOpt;
use structopt::clap::AppSettings;
use std::path;
use std::vec;
use std::fs;
use std::io;

/// The command line interface to the CaSILE toolkit, a book publishing
/// workflow employing SILE and other wizardry
#[derive(StructOpt)]
#[structopt(version = env!("VERGEN_SEMVER"))]
#[structopt(setting = AppSettings::InferSubcommands)]
struct Cli {

/// Activate debug mode
Expand All @@ -15,7 +21,7 @@ struct Cli {
verbose: bool,

#[structopt(subcommand)]
command: Subcommand,
subcommand: Subcommand,

}

Expand All @@ -24,15 +30,19 @@ enum Subcommand {

/// Executes a make target
Make {

/// Target as defined in CaSILE makefile
target: String
target: Vec<String>,

},

/// Configure a book repository
Setup {
#[structopt(default_value = "./")]

/// Path to project repository
path: String
#[structopt(parse(from_os_str), default_value = "./")]
path: path::PathBuf,

},

/// Pass through other commands to shell
Expand All @@ -41,9 +51,55 @@ enum Subcommand {

}

fn main() {
fn main () -> io::Result<()> {

let args = Cli::from_args();
println!("Insert magic potion! (Unimplemented)");

if args.debug {
println!("User requested debug mode")
}

if args.verbose {
println!("User requested verbose output")
}

return match args.subcommand {
Subcommand::Make{ target } => make(target),
Subcommand::Setup{ path } => setup(path),
_a => shell(),
}

}

fn make (_target: vec::Vec<String>) -> io::Result<()> {

println!("Make make make sense or I’ll make you make makefiles.");

Ok(())
}

fn setup (path: path::PathBuf) -> io::Result<()> {

let pathmeta = fs::metadata(path);

match pathmeta {
Ok(file) => {
if file.is_dir() {
println!("Run setup, “They said you were this great colossus!”")
} else {
println!("Not a dir, Frank!")
}
}
Err(error) => { return Err(error.into()) },
};

Ok(())

}

fn shell () -> io::Result<()> {

println!("Ship all this off to the shell, maybe they can handle it.");

Ok(())
}

0 comments on commit 1f46ea2

Please sign in to comment.