Skip to content

Commit

Permalink
cli: transform --init as a flag
Browse files Browse the repository at this point in the history
It makes the --help usage more readable
  • Loading branch information
zimbatm committed Feb 25, 2021
1 parent e05a1e4 commit 72268ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ quite difficult.

```
treefmt 0.1.0
The various kinds of commands that `treefmt` can execute
✨ format all your language!
USAGE:
treefmt [FLAGS] [OPTIONS] [SUBCOMMAND]
treefmt [FLAGS] [OPTIONS] [paths]...
FLAGS:
-h, --help Prints help information
--init Create a new treefmt.toml
-q, --quiet No output printed to stdout
-V, --version Prints version information
-v, --verbose Log verbosity is based off the number of v used
Expand All @@ -60,9 +61,8 @@ OPTIONS:
-C <work-dir> Run as if treefmt was started in <work-dir> instead of the current working directory
[default: .]
SUBCOMMANDS:
--init Init a new project with a default config
help Prints this message or the help of the given subcommand(s)
ARGS:
<paths>... Paths to format [default: .]
```

## Configuration format
Expand Down
13 changes: 7 additions & 6 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub enum Command {
/// ✨ format all your language!
#[derive(Debug, StructOpt)]
pub struct Cli {
/// Main command to run
#[structopt(subcommand)]
pub cmd: Option<Command>,
/// Create a new treefmt.toml
#[structopt(long = "init")]
pub init: bool,

/// Log verbosity is based off the number of v used
#[structopt(long = "verbose", short = "v", parse(from_occurrences))]
Expand Down Expand Up @@ -60,9 +60,10 @@ pub fn cli_from_args() -> anyhow::Result<Cli> {

/// Run a command with the given logger
pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
match cli.cmd {
Some(Command::Init {}) => init_cmd(&cli.work_dir)?,
None => format_cmd(&cli.work_dir, &cli.paths)?,
if cli.init {
init_cmd(&cli.work_dir)?
} else {
format_cmd(&cli.work_dir, &cli.paths)?
}

Ok(())
Expand Down

0 comments on commit 72268ac

Please sign in to comment.