Skip to content

Commit

Permalink
feat(CLI): Show coloured help
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Apr 5, 2021
1 parent 6c62faf commit 62f8eb4
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
33 changes: 19 additions & 14 deletions rust/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{config, convert, logging, open, plugins, serve, upgrade};
use crate::{config, convert, interact, logging, open, plugins, serve, upgrade};
use anyhow::Result;
use regex::Regex;
use structopt::StructOpt;
Expand All @@ -7,12 +7,11 @@ use strum::{Display, EnumVariantNames};
#[derive(Debug, StructOpt)]
#[structopt(
about = "Stencila command line tool",
setting = structopt::clap::AppSettings::DeriveDisplayOrder
setting = structopt::clap::AppSettings::DeriveDisplayOrder,
setting = structopt::clap::AppSettings::ColoredHelp,
setting = structopt::clap::AppSettings::VersionlessSubcommands
)]
pub struct Args {
#[structopt(subcommand)]
pub command: Command,

/// Show debug level log events (and above)
#[structopt(long, conflicts_with_all = &["info", "warn", "error"])]
pub debug: bool,
Expand All @@ -28,6 +27,9 @@ pub struct Args {
/// Show error level log entries only
#[structopt(long, conflicts_with_all = &["debug", "info", "warn"])]
pub error: bool,

#[structopt(subcommand)]
pub command: Option<Command>,
}

#[derive(Debug, Display, StructOpt, EnumVariantNames)]
Expand Down Expand Up @@ -79,7 +81,7 @@ pub async fn cli(args: Vec<String>) -> Result<i32> {
let _logging_guards = logging::init(Some(level), &config.logging)?;

// If not explicitly upgrading then run an upgrade check in the background
let upgrade_thread = if let Command::Upgrade(_) = command {
let upgrade_thread = if let Some(Command::Upgrade(_)) = command {
None
} else {
Some(upgrade::upgrade_auto(&config.upgrade))
Expand All @@ -88,14 +90,17 @@ pub async fn cli(args: Vec<String>) -> Result<i32> {
// Load plugins
plugins::read_plugins()?;

// Run the command
let result = match command {
Command::Open(args) => open::cli::run(args).await,
Command::Convert(args) => convert::cli::run(args),
Command::Serve(args) => serve::cli::run(args, &config.serve).await,
Command::Upgrade(args) => upgrade::cli::run(args, &config.upgrade),
Command::Plugins(args) => plugins::cli::run(args, &config.plugins).await,
Command::Config(args) => config::cli::run(args, &config),
let result = if command.is_none() {
interact::run()
} else {
match command.unwrap() {
Command::Open(args) => open::cli::run(args).await,
Command::Convert(args) => convert::cli::run(args),
Command::Serve(args) => serve::cli::run(args, &config.serve).await,
Command::Plugins(args) => plugins::cli::run(args, &config.plugins).await,
Command::Upgrade(args) => upgrade::cli::run(args, &config.upgrade),
Command::Config(args) => config::cli::run(args, &config),
}
};

// Join the upgrade thread and log any errors
Expand Down
2 changes: 1 addition & 1 deletion rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub mod cli {
#[derive(Debug, StructOpt)]
#[structopt(
about = "Manage configuration options",
setting = structopt::clap::AppSettings::DeriveDisplayOrder
setting = structopt::clap::AppSettings::ColoredHelp
)]
pub struct Args {
#[structopt(subcommand)]
Expand Down
5 changes: 4 additions & 1 deletion rust/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ pub mod cli {
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(about = "Convert a document from one format to another")]
#[structopt(
about = "Convert a document from one format to another",
setting = structopt::clap::AppSettings::ColoredHelp
)]
pub struct Args {
input: String,

Expand Down
5 changes: 4 additions & 1 deletion rust/src/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ pub mod cli {
use super::*;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(about = "Open a document in the browser")]
#[structopt(
about = "Open a document in the browser",
setting = structopt::clap::AppSettings::ColoredHelp
)]
pub struct Args {
/// The file path or URL of the document
#[structopt(default_value = ".")]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ pub mod cli {
#[derive(Debug, StructOpt)]
#[structopt(
about = "Manage plugins",
setting = structopt::clap::AppSettings::DeriveDisplayOrder
setting = structopt::clap::AppSettings::ColoredHelp
)]
pub struct Args {
#[structopt(subcommand)]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub mod cli {
#[derive(Debug, StructOpt)]
#[structopt(
about = "Request a method call on a plugin or peer (mainly for testing)",
setting = structopt::clap::AppSettings::DeriveDisplayOrder
setting = structopt::clap::AppSettings::ColoredHelp
)]
pub struct Args {
/// URL of the peer (e.g. ws://example.org:9001, :9000)
Expand Down
5 changes: 4 additions & 1 deletion rust/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,10 @@ pub mod cli {
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(about = "Serve on HTTP, WebSockets, or Standard I/O")]
#[structopt(
about = "Serve on HTTP, WebSockets, or Standard I/O",
setting = structopt::clap::AppSettings::ColoredHelp
)]
pub struct Args {
/// The URL to serve on (defaults to `ws://127.0.0.1:9000`)
#[structopt(env = "STENCILA_URL")]
Expand Down
5 changes: 4 additions & 1 deletion rust/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ pub mod cli {
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(about = "Upgrade to the latest (or other) version")]
#[structopt(
about = "Upgrade to the latest (or other) version",
setting = structopt::clap::AppSettings::ColoredHelp
)]
pub struct Args {
/// Version to upgrade (or downgrade) to (defaults to the latest)
#[structopt(short, long)]
Expand Down

0 comments on commit 62f8eb4

Please sign in to comment.