How to add long-form subcommand documentation in a clap CLI? - rust

I'm using #[derive(Subcommand)] to introduce subcommands in my CLI:
#[derive(Debug, Subcommand)]
enum Commands {
/// Create a new config file
///
/// Line one
/// Line two
Init,
}
However, the documentation in long form help mode ends up being distorted. More precisely, when running mycli init --help, I get the following documentation output:
Create a new config file
Line one
Line two
Is there a way to add delicately formatted subcommand documentation (in the same way long_about= works for the CLI overall?

You're looking for #[verbatim_doc_comment]:
#[derive(Debug, Subcommand)]
enum Commands {
/// Create a new config file
///
/// Line one
/// Line two
#[command(verbatim_doc_comment)]
Init,
}
Output:
Create a new config file
Line one
Line two
Usage: cli init
Options:
-h, --help
Print help information (use `-h` for a summary)
playground

Related

cargo rust build script - print output of command

I am new to rust and cargo, and I am trying to do something very simple!
I have something like this (in build.rs):
use std::process::Command;
fn main() {
Command::new("echo 123");
}
And I want to see the output of the command echo 123. I want 123 to get printed to the build output (this is mostly to debug what I am doing) and wont be part of the final project.
I have tried cargo build --verbose - this does not work.
I can't extrapolate an answer from there posts (and some others like it):
https://github.com/rust-lang/cargo/issues/985
https://github.com/rust-lang/cargo/issues/1106
I feel this must be simple to do - but I have been stuck for hours looking on the web and not finding the answer.
Just building a Command with Command::new does not execute it yet. It just starts a builder pattern. To actually execute it, you have to use the methods spawn, output or status. Example:
Command::new("echo")
.arg("123")
.spawn()
.expect("failed to spawn process");
It's very unfortunate that this doesn't produce a warning. Someone recently tried to add the #[must_use] attribute to Command, which would make your code procude a warning. The PR closed for now but it seems like it will be added eventually.
We can use a macro and it worked form me, but there is a warning, since it uses cargo to display. but that is fine for me.
I found below code from git hub discussion:
Cargo doesn’t display output from a command in build.rs #985
macro_rules! p {
($($tokens: tt)*) => {
println!("cargo:warning={}", format!($($tokens)*))
}
}
fn main() {
p!("BUILD.rs -> Starting ...");
}

Where do I need to put file to be read by Rust?

When I was reading through the tutorial for Rust here (https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html). I found this block of code:
use std::fs::File;
fn main() {
let f = File::open("hello.txt");
let f = match f {
Ok(file) => file,
Err(error) => panic!("Problem opening the file: {:?}", error),
};
}
It always displays an error: { code: 2, kind: NotFound, message: "The system cannot find the file specified." } even when I make a hello.txt at the root folder of the src, it fails to read it.
In another example here, I use cargo run to no success. The program still fails to read hello.txt file. I'm aware that the example uses rustc open.rs && ./open. Since I don't understand why is it suddenly use different compile method and what's it even mean... I just kinda skip it and try to use cargo run instead
Where do I need to put my file here so cargo run can read it ?
Also if I run the production code and need the program to read an external file, where do I need to put it ?
Here's my folder structure. Pretty simple since I just start to learn RUST.
Thank you in advance.
A file without a directory component in the name needs to be in the current working directory, i.e. the directory from which you start your executable or cargo run.
If you start cargo from an IDE, it might not be immediately apparent what directory it will use as the current directory. In that case, you can always find the current working directory by printing it explicitly:
fn main() {
println!("{}", std::env::current_dir().unwrap().display())
}

How to write a file using LF instead of CRLF on Windows?

We have a script that runs as the preinstall script. It uses fs.writeFile to write a config file which it generates.
writeFile(configFilePath, configFileContents, (e) => {
// ... do some error handling
}
For some reason it uses CRLF line endings on Windows and creating diffs in git although the file has not changed.
I have tried to do use
.replace(/\r\n/gm, "\n");
on configFileContents but it still uses the Windows line endings.
configFileContents gets created by:
const configFileContents = JSON.stringify({
foo: bar,
baz, foo,
// ...
}, null, 2);
Is there a way to tell Node to use the Linux ones?
You can simply do this:
.replace(/\r\n/g, "\n")
Also /\r\n/gm regexp isn't correct as you're already telling the Regexp engine to look for new line by providing the m/multiple lines option... That's why it doesn't allow the expression to work. Just use g if you really wan't to use the RegExp
I used prettier to update the file automatically after the file was created, that worked for me. So i just added prettier command in extention to the file creation npm script.
prettier \"supportedBrowsers.ts\" --write"

Unable to run cucumber feature feature

I am unable to run the feature file. whenever i tried to run the file
i am getting the below stack trace
Exception in thread "main" Usage: java cucumber.api.cli.Main [options] [
[FILE|DIR][:LINE[:LINE]*] ]+
Options:
-g, --glue PATH Where glue code (step definitions and hooks) is loaded from.
-f, --format FORMAT[:PATH_OR_URL] How to format results. Goes to STDOUT unless PATH_OR_URL is specified.
Built-in FORMAT types: junit, html, pretty, progress, json.
FORMAT can also be a fully qualified class name.
-t, --tags TAG_EXPRESSION Only run scenarios tagged with tags matching TAG_EXPRESSION.
-n, --name REGEXP Only run scenarios whose names match REGEXP.
-d, --[no-]-dry-run Skip execution of glue code.
-m, --[no-]-monochrome Don't colour terminal output.
-s, --[no-]-strict Treat undefined and pending steps as errors.
--snippets Snippet name: underscore, camelcase
--dotcucumber PATH_OR_URL Where to write out runtime information. PATH_OR_URL can be a file system
path or a URL.
-v, --version Print version.
-h, --help You're looking at it.
cucumber.runtime.CucumberException: Unknown option: --plugin
at cucumber.runtime.RuntimeOptions.parse(RuntimeOptions.java:119)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:50)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:44)
at cucumber.api.cli.Main.run(Main.java:20)
at cucumber.api.cli.Main.main(Main.java:16)
Please help me to resolve the problem
You normally get this issue if you did not set cucumberOptions correctly on your cukes files.
For example:
#RunWith(Cucumber.class)
#CucumberOptions( dryRun = false, strict = true, features = "src/test/features/com/sample", glue = "com.sample",
tags = { "~#wip", "#executeThis" }, monochrome = true,
format = { "pretty", "html:target/cucumber", "json:target_json/cucumber.json", "junit:taget_junit/cucumber.xml" } )
public class RunCukeTest {
}
Hi I also had this issue as well, and I did the following to resolve it, thanks to the comments of Anusha from video https://youtu.be/pD4B839qfos
-the main trick is to firstly change the jar files you have as follows
cucumber-core-1.2.5.jar
cucumber-java-1.2.5.jar
cucumber-junit-1.2.5.jar
or any of the above, from 1.2.4 upwards
- also update the following selenium-server-standalone-2.42.0.jar and upwards
- also change the format keyword to plugin
Once you make the above changes, this should resolve your problem.

How to create a JSCS config file on windows

When I try to create a JSCS config file:
C:\Blog\BlogWeb>jscs --auto-configure "C:\Blog\BlogWeb\temp.jscs"
I get the following error:
safeContextKeyword option requires string or array value
What parameter am I supposed to pass? What is a safecontextkeyword?
New to NPM and JSCS, please excuse ignorance.
JSCS was complaining that I didn't have a config file, so I was trying to figure out how to create one.
JSCS looks for a config file in these places, unless you manually specify it with the --config option:
jscs it will consequentially search for jscsConfig option in package.json file then for .jscsrc (which is a just JSON with comments) and .jscs.json files in the current working directory then in nearest ancestor until it hits the system root.
I fixed this by:
Create a new file named .jscsrc. Windows Explorer may not let you do this, so may need to use the command line.
Copy the following into it. It doesn't matter if this is the preset you want to use or not. The command will overwrite it.
{
"preset": "jquery",
"requireCurlyBraces": null // or false
}
Verify that it works by running a command such as:
run the command
jscs --auto-configure .jscsrc

Resources