-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the CLI functionality into the library (#326)
* Move the CLI functionality into the library * comments * fix * fix todo * pub root * todo * dont treat help as error * empty commit
- Loading branch information
1 parent
77f3744
commit 5dcb7a4
Showing
9 changed files
with
177 additions
and
160 deletions.
There are no files selected for viewing
This file contains 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,75 +1,14 @@ | ||
mod decode; | ||
mod encode; | ||
mod guess; | ||
mod types; | ||
mod version; | ||
|
||
use clap::{CommandFactory, Parser, Subcommand, ValueEnum}; | ||
use std::{error::Error, fmt::Debug}; | ||
|
||
#[derive(Parser, Debug, Clone)] | ||
#[command( | ||
author, | ||
version, | ||
about, | ||
long_about = None, | ||
disable_help_subcommand = true, | ||
disable_version_flag = true, | ||
disable_colored_help = true, | ||
infer_subcommands = true, | ||
)] | ||
struct Root { | ||
/// Channel of XDR to operate on | ||
#[arg(value_enum, default_value_t)] | ||
channel: Channel, | ||
#[command(subcommand)] | ||
cmd: Cmd, | ||
} | ||
|
||
#[derive(ValueEnum, Debug, Clone)] | ||
pub enum Channel { | ||
#[value(name = "+curr")] | ||
Curr, | ||
#[value(name = "+next")] | ||
Next, | ||
} | ||
|
||
impl Default for Channel { | ||
fn default() -> Self { | ||
Self::Curr | ||
} | ||
} | ||
|
||
#[derive(Subcommand, Debug, Clone)] | ||
enum Cmd { | ||
/// View information about types | ||
Types(types::Cmd), | ||
/// Guess the XDR type | ||
Guess(guess::Cmd), | ||
/// Decode XDR | ||
Decode(decode::Cmd), | ||
/// Encode XDR | ||
Encode(encode::Cmd), | ||
/// Print version information | ||
Version, | ||
} | ||
|
||
fn run() -> Result<(), Box<dyn Error>> { | ||
let root = Root::parse(); | ||
match root.cmd { | ||
Cmd::Types(c) => c.run(&root.channel)?, | ||
Cmd::Guess(c) => c.run(&root.channel)?, | ||
Cmd::Decode(c) => c.run(&root.channel)?, | ||
Cmd::Encode(c) => c.run(&root.channel)?, | ||
Cmd::Version => version::Cmd::run(), | ||
} | ||
Ok(()) | ||
} | ||
use clap::Error; | ||
use std::env; | ||
use stellar_xdr::cli; | ||
|
||
fn main() { | ||
if let Err(e) = run() { | ||
Root::command() | ||
.error(clap::error::ErrorKind::ValueValidation, e) | ||
.exit() | ||
if let Err(e) = cli::run(env::args_os()) { | ||
match e { | ||
cli::Error::Clap(e) => e.exit(), | ||
cli::Error::Guess(_) | cli::Error::Decode(_) | cli::Error::Encode(_) => { | ||
Error::raw(clap::error::ErrorKind::ValueValidation, e).exit() | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.