Browse Source

Split the parsing into its own module

master
Julio Biason 4 years ago
parent
commit
69d7133a6b
  1. 5
      Cargo.toml
  2. 93
      src/args.rs
  3. 110
      src/main.rs

5
Cargo.toml

@ -1,10 +1,9 @@
[package]
name = "nrp"
description = "(Name Rust Program) creates a name for your application from a short description"
version = "0.1.0"
authors = ["Julio Biason <julio.biason@pm.me>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "2.33.0"
clap = "2.33"

93
src/args.rs

@ -0,0 +1,93 @@
/*
NRP - Name Rust Program
Copyright (C) 2020 Julio Biason
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use clap::crate_authors;
use clap::crate_description;
use clap::crate_name;
use clap::crate_version;
use clap::App;
use clap::Arg;
// use clap::ArgMatches;
use clap::SubCommand;
#[derive(Debug)]
pub enum Action {
Generate(String),
}
#[derive(Debug)]
pub enum ParseError {
UnknownCommand,
MissingDescription,
}
pub fn parse() -> Result<Action, ParseError> {
let params = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.subcommand(
SubCommand::with_name("generate")
.about("Generate a name")
.arg(
Arg::with_name("description")
.required(true)
.help("Short description of your application"),
),
)
.subcommand(
SubCommand::with_name("adjectives")
.about("Manage the adjective list")
.subcommand(SubCommand::with_name("list").about("List current adjectives"))
.subcommand(
SubCommand::with_name("add")
.about("Add an adjective")
.arg(Arg::with_name("adjective").help("Adjecive to be added")),
)
.subcommand(
SubCommand::with_name("rm")
.about("Remove an adjective")
.arg(Arg::with_name("adjective").help("Adjective to be removed")),
),
)
.subcommand(
SubCommand::with_name("metals")
.about("Manage the metal list")
.subcommand(SubCommand::with_name("list").about("List all metals"))
.subcommand(
SubCommand::with_name("add")
.about("Add a metal to the list")
.arg(Arg::with_name("metal").help("Metal name to be added")),
)
.subcommand(
SubCommand::with_name("rm")
.about("Remove a metal from the list")
.arg(Arg::with_name("metal").help("Metal name to be removed")),
),
);
let matches = params.get_matches();
match matches.subcommand() {
("generate", Some(arguments)) => {
let description = arguments
.value_of("description")
.ok_or(ParseError::MissingDescription)?;
Ok(Action::Generate(description.to_string()))
}
_ => Err(ParseError::UnknownCommand),
}
}

110
src/main.rs

@ -1,98 +1,26 @@
use clap::{Arg, ArgMatches, App, SubCommand};
/*
NRP - Name Rust Program
Copyright (C) 2020 Julio Biason
fn main() {
let generate = SubCommand::with_name("generate")
.about("Generate a new name")
.arg(Arg::with_name("description")
.required(true)
.help("Short description of your application"));
let adjectives = SubCommand::with_name("adjectives")
.about("Adjectives maintenance")
.subcommand(SubCommand::with_name("list")
.about("List current adjectives"))
.subcommand(SubCommand::with_name("add")
.about("Add a new adjective")
.arg(Arg::with_name("adjective")
.help("Adjective to be added")))
.subcommand(SubCommand::with_name("rm")
.about("Remove an adjective")
.arg(Arg::with_name("adjective")
.help("Adjective to be removed")));
let metals = SubCommand::with_name("metals")
.about("Metal names maintenance")
.subcommand(SubCommand::with_name("list")
.about("List current metal names"))
.subcommand(SubCommand::with_name("add")
.about("Add a new metal name")
.arg(Arg::with_name("name")
.help("Metal name to be added")))
.subcommand(SubCommand::with_name("rm")
.about("Remove a metal name")
.arg(Arg::with_name("name")
.help("Metal name to be removed")));
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
let main = App::new("Name Rust Programs")
.version("0.1")
.author("Julio Biason <julio.biason@pm.me>")
.about("From a short description, create a name for your application")
.subcommand(generate)
.subcommand(adjectives)
.subcommand(metals);
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
let matches = main.get_matches();
match matches.subcommand() {
("metals", Some(metal_command)) => metals_commands(&metal_command),
("adjectives", Some(adj_command)) => adjective_commands(&adj_command),
("generate", Some(gen_command)) => generate_command(&gen_command),
(_, _) => panic!("I don't know how to handle that!"),
};
}
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
fn metals_commands(command: &ArgMatches) {
match command.subcommand() {
("list", Some(_)) => metals_list(),
("add", Some(add_cmd)) => metals_add(add_cmd.value_of("name")),
("rm", Some(rm_cmd)) => metals_rm(rm_cmd.value_of("name")),
(command, _) => println!("Invalid command: {}", command),
}
}
mod args;
fn adjective_commands(command: &ArgMatches) {
match command.subcommand() {
("list" , Some(_)) => adjective_list(),
("add" , Some(add_cmd)) => adjective_add(add_cmd.value_of("name")),
("rm" , Some(rm_cmd)) => adjective_rm(rm_cmd.value_of("name")),
(command, _) => println!("Invalid command: {}", command),
fn main() {
match args::parse() {
Ok(x) => println!("Ok: {:?}", x),
Err(x) => println!("Error {:?}", x),
}
}
fn generate_command(command: &ArgMatches) {
let app_description = command.value_of("description");
dbg!("generate description", app_description);
}
fn metals_list() {
dbg!("list metals");
}
fn metals_add(name: Option<&str>) {
dbg!("Add metal", name);
}
fn metals_rm(name: Option<&str>) {
dbg!("Rmove metal", name);
}
fn adjective_list() {
dbg!("list adjectives");
}
fn adjective_add(name: Option<&str>) {
dbg!("Add adjective", name);
}
fn adjective_rm(name: Option<&str>) {
dbg!("Remove adjective", name);
}

Loading…
Cancel
Save