Add basic clap scaffolding a la dnsi

This commit is contained in:
Jannik Peters
2024-08-29 10:51:44 +02:00
parent 729a08089e
commit bd0c9d7118
8 changed files with 117 additions and 13 deletions
-13
View File
@@ -1,21 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "dnst"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4", features = ["derive"] }
+15
View File
@@ -0,0 +1,15 @@
use super::commands::Command;
use super::error::Error;
#[derive(Clone, Debug, clap::Parser)]
#[command(version, disable_help_subcommand = true)]
pub struct Args {
#[command(subcommand)]
command: Command,
}
impl Args {
pub fn execute(self) -> Result<(), Error> {
self.command.execute()
}
}
+13
View File
@@ -0,0 +1,13 @@
use crate::error::Error;
#[derive(Clone, Debug, clap::Args)]
pub struct Help {
#[arg(value_name = "COMMAND")]
command: Option<String>,
}
impl Help {
pub fn execute(self) -> Result<(), Error> {
Ok(())
}
}
+19
View File
@@ -0,0 +1,19 @@
//! The command of _dnst_.
pub mod help;
use super::error::Error;
#[derive(Clone, Debug, clap::Subcommand)]
pub enum Command {
/// Show the manual pages
Help(self::help::Help),
}
impl Command {
pub fn execute(self) -> Result<(), Error> {
match self {
Self::Help(help) => help.execute(),
}
}
}
+51
View File
@@ -0,0 +1,51 @@
use std::borrow::Cow;
use std::num::ParseIntError;
use std::string::ParseError;
use std::{error, fmt, io};
#[derive(Clone, Debug)]
pub struct Error {
message: Cow<'static, str>,
}
impl From<&'static str> for Error {
fn from(message: &'static str) -> Self {
Self {
message: Cow::Borrowed(message),
}
}
}
impl From<String> for Error {
fn from(message: String) -> Self {
Self {
message: Cow::Owned(message),
}
}
}
impl From<ParseError> for Error {
fn from(_err: ParseError) -> Self {
Self::from("message parse error")
}
}
impl From<ParseIntError> for Error {
fn from(_err: ParseIntError) -> Self {
Self::from("message parse error")
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::from(err.to_string())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.message, f)
}
}
impl error::Error for Error {}
+5
View File
@@ -0,0 +1,5 @@
pub use self::args::Args;
pub mod args;
pub mod commands;
pub mod error;
+7
View File
@@ -0,0 +1,7 @@
use clap::Parser;
fn main() {
if let Err(err) = dnst::Args::parse().execute() {
eprintln!("{}", err);
}
}