summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Gimbel <hallo@kevingimbel.com>2020-02-28 11:45:57 +0100
committerKevin Gimbel <hallo@kevingimbel.com>2020-02-28 11:45:57 +0100
commit62d24225454c3d585ff95211672ce0cb886acf0e (patch)
treee7946bbc25a52cc8e0b3a8153bb93e4b47a4229d
parent49d52a93a048cedf5191c0a10658ccbece4d6129 (diff)
feat: Defaults for all parameters
-rw-r--r--Cargo.toml2
-rw-r--r--README.md64
-rw-r--r--src/bin.rs28
3 files changed, 41 insertions, 53 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 5418903..a093bd9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,7 +2,7 @@
name = "mktoc"
description = "Generate Table of Contents from Markdown files"
license = "MIT"
-version = "1.1.2"
+version = "2.0.0"
authors = ["Kevin Gimbel <hallo@kevingimbel.com>"]
edition = "2018"
diff --git a/README.md b/README.md
index ece8ca9..011f76c 100644
--- a/README.md
+++ b/README.md
@@ -4,75 +4,47 @@
![](https://github.com/kevingimbel/mktoc/workflows/Clippy%20check/badge.svg)
<!-- BEGIN mktoc -->
-- [About](#about)
-- [Installation](#installation)
- - [Cargo](#cargo)
- - [Binary](#binary)
-- [Usage](#usage)
- - [Update Markdown file](#update-markdown-file)
+ - [Command line](#command-line)
+ - [Environment configuration](#environment-configuration)
- [Performance](#performance)
- [License](#license)
<!-- END mktoc -->
-
-## About
-
-`mktoc` parses markdown files and generates a Table Of Content linking all headlines up to heading level 6 deep, or as specified by command line arguments. A start depth and maximum depth can be specified.
-
-## Installation
-
-`mktoc` can be installed using Cargo, the Rust package manager.
-
-### Cargo
-
-```sh
-$ cargo install mktoc
-```
-
-### Binary
-
-Binaries are actually not available yet. If you know how releasing binaries with Rust can be implemented, please let me know!
-
-## Usage
-
-### Update Markdown file
-
-Add the following HTML comment into the Markdown file where the Table of Contents should be rendered.
-```
-<!-- BEGIN mktoc -->
-<!-- END mktoc -->
```
Everything between those comments will be replaced!
### Command line
-Specify `--write` to overwrite the given file, otherwise the modified content is written to stdout.
+Specify `--stdout` or `-s` to output generated content to `stdout` instead of overwriting file. By default the specified file will be overwritten.
```
-# mktoc [--write] [--max-depth|-M] [--min-depth|-m] <FILE>
-$ mktoc --write README.md
-$ mktoc --write -m 2 -M 4 README.md
+# mktoc [FLAGS] [OPTIONS] [file]
+$ mktoc -s README.md
+$ mktoc -m 2 -M 4 README.md
+$ mktoc
```
+If no arguments are given the default or configured (via environment) values are
+used.
See `mktoc --help` for list of all arguments and flags.
```
-mktoc 1.1.0
+mktoc
USAGE:
- mktoc [FLAGS] [OPTIONS] <file>
+mktoc [FLAGS] [OPTIONS] [file]
FLAGS:
- -h, --help Prints help information
- -V, --version Prints version information
- -w, --write
+-h, --help Prints help information
+-s, --stdout If set will output to stdout instead of replacing content in file
+-V, --version Prints version information
OPTIONS:
- -M, --max-depth <max-depth> [default: 6]
- -m, --min-depth <min-depth> [default: 1]
+-M, --max-depth <max-depth> Maximum heading level [env: MKTOC_MAX_DEPTH=] [default: 6]
+-m, --min-depth <min-depth> Minimum heading level [env: MKTOC_MIN_DEPTH=2] [default: 1]
ARGS:
- <file>
+<file> [default: README.md]
```
### Environment configuration
@@ -92,7 +64,7 @@ Place these variables in a shell environment file such as `~/.bashrc` or
# MKTOC_MIN_DEPTH=2
# MKTOC_MAX_DEPTH=4
-$ mktoc --write README.md
+$ mktoc README.md
```
## Performance
diff --git a/src/bin.rs b/src/bin.rs
index 3ebb7b6..f31bd46 100644
--- a/src/bin.rs
+++ b/src/bin.rs
@@ -3,22 +3,38 @@ use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "mktoc")]
struct Cli {
- #[structopt()]
+ #[structopt(default_value = "README.md")]
file: String,
- #[structopt(long, short)]
- write: bool,
+ #[structopt(
+ long,
+ short,
+ help = "If set will output to stdout instead of replacing content in file"
+ )]
+ stdout: bool,
- #[structopt(long, short = "m", default_value = "1", env="MKTOC_MIN_DEPTH")]
+ #[structopt(
+ long,
+ short = "m",
+ default_value = "1",
+ env = "MKTOC_MIN_DEPTH",
+ help = "Minimum heading level"
+ )]
min_depth: i32,
- #[structopt(long, short = "M", default_value = "6", env="MKTOC_MAX_DEPTH")]
+ #[structopt(
+ long,
+ short = "M",
+ default_value = "6",
+ env = "MKTOC_MAX_DEPTH",
+ help = "Maximum heading level"
+ )]
max_depth: i32,
}
fn handle_write(new_toc: String) {
let opts = Cli::from_args();
- if opts.write {
+ if !opts.stdout {
let res_write = std::fs::write(opts.file, new_toc.as_bytes());
match res_write {
Ok(_r) => {