Skip to content

Commit

Permalink
Update arg parsing to clap v3 (#1717)
Browse files Browse the repository at this point in the history
* Update arg parsing to clap v3
  • Loading branch information
tranzystorekk authored Jan 23, 2022
1 parent 6238b36 commit c9b2d35
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 186 deletions.
93 changes: 89 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ keywords = ["static", "site", "generator", "blog"]
include = ["src/**/*", "LICENSE", "README.md"]

[build-dependencies]
clap = "2"
clap = "3"
clap_complete = "3"

[[bin]]
name = "zola"

[dependencies]
atty = "0.2.11"
clap = { version = "2", default-features = false }
clap = { version = "3", features = ["derive"] }
chrono = "0.4"
lazy_static = "1.1"
termcolor = "1.0.4"
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ stages:
rustup_toolchain: stable
linux-pinned:
imageName: 'ubuntu-20.04'
rustup_toolchain: 1.53.0
rustup_toolchain: 1.54.0
pool:
vmImage: $(imageName)
steps:
Expand Down
186 changes: 85 additions & 101 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,102 +1,86 @@
use clap::{crate_authors, crate_description, crate_version, App, AppSettings, Arg, SubCommand};

pub fn build_cli() -> App<'static, 'static> {
App::new("zola")
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.setting(AppSettings::SubcommandRequiredElseHelp)
.arg(
Arg::with_name("root")
.short("r")
.long("root")
.takes_value(true)
.default_value(".")
.help("Directory to use as root of project")
)
.arg(
Arg::with_name("config")
.short("c")
.long("config")
.takes_value(true)
.help("Path to a config file other than config.toml in the root of project")
)
.subcommands(vec![
SubCommand::with_name("init")
.about("Create a new Zola project")
.args(&[
Arg::with_name("name")
.default_value(".")
.help("Name of the project. Will create a new directory with that name in the current directory"),
Arg::with_name("force")
.short("f")
.long("force")
.takes_value(false)
.help("Force creation of project even if directory is non-empty")
]),
SubCommand::with_name("build")
.about("Deletes the output directory if there is one and builds the site")
.args(&[
Arg::with_name("base_url")
.short("u")
.long("base-url")
.takes_value(true)
.help("Force the base URL to be that value (default to the one in config.toml)"),
Arg::with_name("output_dir")
.short("o")
.long("output-dir")
.takes_value(true)
.help("Outputs the generated site in the given path (by default 'public' dir in project root)"),
Arg::with_name("drafts")
.long("drafts")
.takes_value(false)
.help("Include drafts when loading the site"),
]),
SubCommand::with_name("serve")
.about("Serve the site. Rebuild and reload on change automatically")
.args(&[
Arg::with_name("interface")
.short("i")
.long("interface")
.takes_value(true)
.help("Interface to bind on (default: 127.0.0.1)"),
Arg::with_name("port")
.short("p")
.long("port")
.takes_value(true)
.help("Which port to use (default: 1111)"),
Arg::with_name("output_dir")
.short("o")
.long("output-dir")
.takes_value(true)
.help("Outputs assets of the generated site in the given path (by default 'public' dir in project root). HTML/XML will be stored in memory."),
Arg::with_name("base_url")
.short("u")
.long("base-url")
.takes_value(true)
.help("Changes the base_url (default: 127.0.0.1)"),
Arg::with_name("drafts")
.long("drafts")
.takes_value(false)
.help("Include drafts when loading the site"),
Arg::with_name("open")
.short("O")
.long("open")
.takes_value(false)
.help("Open site in the default browser"),
Arg::with_name("fast")
.short("f")
.long("fast")
.takes_value(false)
.help("Only rebuild the minimum on change - useful when working on a specific page/section"),
]),
SubCommand::with_name("check")
.about("Try building the project without rendering it. Checks links")
.args(&[
Arg::with_name("drafts")
.long("drafts")
.takes_value(false)
.help("Include drafts when loading the site"),
])
])
use std::path::PathBuf;

use clap::{Parser, Subcommand};

#[derive(Parser)]
#[clap(version, author, about)]
pub struct Cli {
/// Directory to use as root of project
#[clap(short = 'r', long, default_value = ".")]
pub root: PathBuf,

/// Path to a config file other than config.toml in the root of project
#[clap(short = 'c', long)]
pub config: Option<PathBuf>,

#[clap(subcommand)]
pub command: Command,
}

#[derive(Subcommand)]
pub enum Command {
/// Create a new Zola project
Init {
/// Name of the project. Will create a new directory with that name in the current directory
#[clap(default_value = ".")]
name: String,

/// Force creation of project even if directory is non-empty
#[clap(short = 'f', long)]
force: bool,
},

/// Deletes the output directory if there is one and builds the site
Build {
/// Force the base URL to be that value (defaults to the one in config.toml)
#[clap(short = 'u', long)]
base_url: Option<String>,

/// Outputs the generated site in the given path (by default 'public' dir in project root)
#[clap(short = 'o', long)]
output_dir: Option<PathBuf>,

/// Include drafts when loading the site
#[clap(long)]
drafts: bool,
},

/// Serve the site. Rebuild and reload on change automatically
Serve {
/// Interface to bind on
#[clap(short = 'i', long, default_value = "127.0.0.1")]
interface: String,

/// Which port to use
#[clap(short = 'p', long, default_value_t = 1111)]
port: u16,

/// Outputs assets of the generated site in the given path (by default 'public' dir in project root).
/// HTML/XML will be stored in memory.
#[clap(short = 'o', long)]
output_dir: Option<PathBuf>,

/// Changes the base_url
#[clap(short = 'u', long, default_value = "127.0.0.1")]
base_url: String,

/// Include drafts when loading the site
#[clap(long)]
drafts: bool,

/// Open site in the default browser
#[clap(short = 'O', long)]
open: bool,

/// Only rebuild the minimum on change - useful when working on a specific page/section
#[clap(short = 'f', long)]
fast: bool,
},

/// Try to build the project without rendering it. Checks links
Check {
/// Include drafts when loading the site
#[clap(long)]
drafts: bool,
},
}
Loading

0 comments on commit c9b2d35

Please sign in to comment.