Update hurlfmt usage

This commit is contained in:
Fabrice Reix 2020-11-15 09:51:01 +01:00
parent d0b31ee7ff
commit b9f1efd174
4 changed files with 136 additions and 89 deletions

View File

@ -5,35 +5,105 @@ hurlfmt - format Hurl files
## SYNOPSIS
**hurlfmt** [options] [FILE...]
**hurlfmt** [options] [FILE]
## DESCRIPTION
**hurlfmt** formats Hurl files.
**hurlfmt** formats Hurl files and converts to other formats.
With no FILE, read standard input.
By default, hurlfmt outputs a formatted and colorized version of the input hurl file.
```
$ hurl hello.hurl
GET http://localhost:8000/hello
HTTP/1.0 200
```
hurlfmt can be used to convert to other format.
```
$ hurl hello.hurl --output json | jq
{
"entries": [
{
"request": {
"method": "GET",
"url": "http://localhost:8000/hello"
},
"response": {
"version": "HTTP/1.0",
"status": 200
}
}
]
}
```
## OPTIONS
### --check {#check}
Run in 'check' mode. Exits with 0 if input is formatted correctly, 1 otherwise.
This can not be used with [--output](#output).
This option is not stable yet.
### --color {#color}
Colorize Output
Colorize Output.
This can not be used [--in-place](#inplace).
### -h, --help {#help}
Usage help. This lists all current command line options with a short description.
Usage help.
### --inplace {#inplace}
### --html {#html}
Modify file in place.
Generate html output.
This can be used only with text output.
### --no-color {#nocolor}
Do not colorize Output.
### --no-format {#noformat}
Do not format output.
By default text output is automatically formatted.
### --output {#output}
Specify output format: text (default), json or html.
### --standalone {#standalone}
Output full html file with css instead of html fragment (default).
This can be used only with html output.
### -V, --version {#version}
@ -45,10 +115,12 @@ Prints version information
## EXIT CODES
### 1
Failed to parse command-line options.
### 2
Input File Parsing Error.

View File

@ -3,9 +3,9 @@ set -eo pipefail
for hurl_file in "$@"; do
html_file="${hurl_file%.*}".html
cmd="hurlfmt --html $hurl_file"
cmd="hurlfmt --output html $hurl_file"
echo "$cmd"
$cmd 2>/tmp/test.stderr >/tmp/test.stdout
$cmd >/tmp/test.stdout
expected=$(cat "$html_file")
actual=$(cat /tmp/test.stdout)

View File

@ -4,10 +4,10 @@ set -u
set -eo pipefail
for hurl_file in "$@"; do
json_file="${hurl_file%.*}.json"
cmd="hurlfmt --json $hurl_file"
cmd="hurlfmt --output json $hurl_file"
echo "$cmd"
$cmd 2>/tmp/test.stderr >/tmp/test.stdout
$cmd >/tmp/test.stdout
expected=$(cat "$json_file")
actual=$(cat /tmp/test.stdout)
if [ "$actual" != "$expected" ]; then

View File

@ -30,34 +30,36 @@ use hurlfmt::cli;
use hurlfmt::format;
use hurlfmt::linter::Lintable;
pub struct CLIError {
pub message: String,
}
fn main() {
// // Do we have a git hash?
// // (Yes, if ripgrep was built on a machine with `git` installed.)
// let hash = match revision_hash.or(option_env!("HURL_BUILD_GIT_HASH")) {
// None => String::new(),
// Some(githash) => format!(" (rev {})", githash),
// };
let app = clap::App::new("hurlfmt")
// .author(clap::crate_authors!())
.version(clap::crate_version!())
.about("Format hurl FILE or standard input")
.about("Format hurl FILE")
.arg(
clap::Arg::with_name("INPUT")
.help("Sets the input file to use")
.required(false)
.index(1),
)
.arg(
clap::Arg::with_name("check")
.long("check")
.conflicts_with("output")
.help("Run in 'check' mode"),
)
.arg(
clap::Arg::with_name("color")
.long("color")
.conflicts_with("no-color")
.conflicts_with("no_color")
.conflicts_with("in_place")
.help("Colorize Output"),
)
.arg(
clap::Arg::with_name("in_place")
.long("in-place")
.conflicts_with("output")
.help("Modify file in place"),
)
.arg(
clap::Arg::with_name("no_color")
.long("no-color")
@ -70,60 +72,24 @@ fn main() {
.help("Do not format Output"),
)
.arg(
clap::Arg::with_name("html_output")
.long("html")
.conflicts_with("ast_output")
.conflicts_with("json_output")
.help("Output Html"),
)
.arg(
clap::Arg::with_name("json_output")
.long("json")
.conflicts_with("ast_output")
.conflicts_with("html_output")
.help("Output Json"),
clap::Arg::with_name("output")
.long("output")
.conflicts_with("check")
.value_name("FORMAT")
.help("Output to a specified format: text (default), json or html"),
)
.arg(
clap::Arg::with_name("standalone")
.long("standalone")
.conflicts_with("ast_output")
.help("Standalone Html"),
)
.arg(
clap::Arg::with_name("html_css_output")
.long("html-css")
.conflicts_with("ast_output")
.help("Output Html"),
)
.arg(
clap::Arg::with_name("ast_output")
.long("ast")
.conflicts_with("html_output")
.conflicts_with("json_output")
.help("Output AST"),
)
.arg(
clap::Arg::with_name("check")
.long("check")
.conflicts_with("ast_output")
.conflicts_with("html_output")
.help("Run in 'check' mode. Exits with 0 if input is\nformatted correctly. Exits with 1 and prints a diff if\nformatting is required"),
)
.arg(
clap::Arg::with_name("in_place")
.long("in-place")
.conflicts_with("ast_output")
.conflicts_with("html_output")
.conflicts_with("color")
.help("Modify file in place"),
);
let matches = app.clone().get_matches();
// can you do this check directly with clap
if matches.is_present("standalone") && !matches.is_present("html_output") {
eprintln!("use standalone option only with html output");
// Additional checks
if matches.is_present("standalone") && matches.value_of("output") != Some("html") {
eprintln!("use --standalone option only with html output");
std::process::exit(1);
}
@ -194,30 +160,39 @@ fn main() {
log_linter_error(&e, true);
}
std::process::exit(1);
} else if matches.is_present("ast_output") {
eprintln!("{:#?}", hurl_file);
} else if matches.is_present("json_output") {
println!("{}", format::format_json(hurl_file));
} else if matches.is_present("html_output") {
let standalone = matches.is_present("standalone");
println!("{}", format::format_html(hurl_file, standalone));
} else {
let hurl_file = if matches.is_present("no_format") {
hurl_file
} else {
hurl_file.lint()
};
if matches.is_present("in_place") {
match fs::File::create(filename) {
Ok(mut f) => {
let s = format::format_text(hurl_file, false);
f.write_all(s.as_bytes()).unwrap();
match matches.value_of("output").unwrap_or("text") {
"text" => {
let hurl_file = if matches.is_present("no_format") {
hurl_file
} else {
hurl_file.lint()
};
if matches.is_present("in_place") {
match fs::File::create(filename) {
Ok(mut f) => {
let s = format::format_text(hurl_file, false);
f.write_all(s.as_bytes()).unwrap();
}
Err(_) => {
eprintln!("Error opening file {} in write mode", filename)
}
};
} else {
print!("{}", format::format_text(hurl_file, output_color));
}
Err(_) => eprintln!("Error opening file {} in write mode", filename),
};
} else {
print!("{}", format::format_text(hurl_file, output_color));
};
}
"json" => println!("{}", format::format_json(hurl_file)),
"html" => {
let standalone = matches.is_present("standalone");
println!("{}", format::format_html(hurl_file, standalone));
}
"ast" => println!("{:#?}", hurl_file),
_ => {
eprintln!("Invalid output option - expecting text, html or json");
std::process::exit(1);
}
}
}
}
}