Skip to content

Commit 7bcac37

Browse files
authored
Improve cli_examples macro (#5589)
## Description Add macro to test argument parsing rather than testing the external command through building and spawning a separated process. This is an improved version of #5519 ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
1 parent 6fa3878 commit 7bcac37

36 files changed

+237
-292
lines changed

forc-plugins/forc-client/src/cmd/deploy.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ pub use forc_util::tx_utils::Salt;
88
use crate::NodeTarget;
99

1010
forc_util::cli_examples! {
11-
[ Deploy a single contract => deploy "bc09bfa7a11a04ce42b0a5abf04fd437387ee49bf4561d575177e2946468b408" => r#".*Error making HTTP request.*"# ]
12-
[ Deploy a single contract from a different path => deploy "bc09bfa7a11a04ce42b0a5abf04fd437387ee49bf4561d575177e2946468b408 --path ../tests/" => r#".*Error making HTTP request.*"# ]
13-
[ Deploy to a custom network => deploy "--node-url https://beta-5.fuel.network/graphql" => ".*Refused to create a new wallet.*" ]
11+
super::Command {
12+
[ Deploy a single contract => "forc deploy bc09bfa7a11a04ce42b0a5abf04fd437387ee49bf4561d575177e2946468b408" ]
13+
[ Deploy a single contract from a different path => "forc deploy bc09bfa7a11a04ce42b0a5abf04fd437387ee49bf4561d575177e2946468b408 --path {path}" ]
14+
[ Deploy to a custom network => "forc deploy --node-url https://beta-5.fuel.network/graphql" ]
15+
}
1416
}
1517

1618
#[derive(Debug, Default, Parser)]

forc-plugins/forc-client/src/cmd/submit.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ use devault::Devault;
33
use std::path::PathBuf;
44

55
forc_util::cli_examples! {
6-
[ Submit a transaction from a json file => submit "./mint.json" => "Submission of tx or awaiting commit failed" ]
7-
[ Submit a transaction from a json file and wait for confirmation => submit "./mint.json --await true" => "Submission of tx or awaiting commit failed" ]
8-
[ Submit a transaction from a json file and get output in json => submit "./mint.json --tx-status-json true" => "Submission of tx or awaiting commit failed" ]
9-
[ Submit a transaction from a json file to testnet => submit "./mint.json --testnet" => "Submission of tx or awaiting commit failed" ]
10-
[ Submit a transaction from a json file to a local net => submit "./mint.json --target local" => "Submission of tx or awaiting commit failed" ]
6+
super::Command {
7+
[ Submit a transaction from a json file => "forc submit {path}/mint.json" ]
8+
[ Submit a transaction from a json file and wait for confirmation => "forc submit {path}/mint.json --await true" ]
9+
[ Submit a transaction from a json file and get output in json => "forc submit {path}/mint.json --tx-status-json true" ]
10+
[ Submit a transaction from a json file to testnet => "forc submit {path}/mint.json --testnet" ]
11+
[ Submit a transaction from a json file to a local net => "forc submit {path}/mint.json --target local" ]
12+
}
1113
}
1214

1315
/// Submit a transaction to the specified fuel node.

forc-plugins/forc-client/tests/.gitignore

-2
This file was deleted.

forc-plugins/forc-client/tests/Forc.lock

-13
This file was deleted.

forc-plugins/forc-client/tests/Forc.toml

-8
This file was deleted.

forc-plugins/forc-client/tests/mint.json

-29
This file was deleted.

forc-plugins/forc-client/tests/src/main.sw

-11
This file was deleted.

forc-plugins/forc-crypto/src/address.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use serde_json::json;
55
use std::str::{from_utf8, FromStr};
66

77
forc_util::cli_examples! {
8-
[ Convert an address to another format => crypto "address fuel12e0xwx34nfp7jrzvn9mp5qkac3yvp7h8fx37ghl7klf82vv2wkys6wd523" ]
8+
crate::Command {
9+
[ Convert an address to another format => "forc crypto address fuel12e0xwx34nfp7jrzvn9mp5qkac3yvp7h8fx37ghl7klf82vv2wkys6wd523" ]
10+
}
911
}
1012

1113
#[derive(Debug, clap::Args)]

forc-plugins/forc-crypto/src/args.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ use std::{
77
};
88

99
forc_util::cli_examples! {
10-
[ Hashes an argument with SHA256 => crypto "sha256 test" ]
11-
[ Hashes an argument with Keccak256 => crypto "keccak256 test" ]
12-
[ Hashes a file path with SHA256 => crypto "sha256 src/args.rs" ]
13-
[ Hashes a file path with Keccak256 => crypto "keccak256 src/args.rs" ]
10+
crate::Command {
11+
[ Hashes an argument with SHA256 => "forc crypto sha256 test" ]
12+
[ Hashes an argument with Keccak256 => "forc crypto keccak256 test" ]
13+
[ Hashes a file path with SHA256 => "forc crypto sha256 {file}" ]
14+
[ Hashes a file path with Keccak256 => "forc crypto keccak256 {file}" ]
15+
}
1416
}
1517

1618
#[derive(Debug, Clone, clap::Args)]

forc-plugins/forc-crypto/src/keys/get_public_key.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ use fuels_core::types::bech32::Bech32Address;
55
use serde_json::json;
66

77
forc_util::cli_examples! {
8-
[ Get the public key from a message and its signature => crypto r#"get-public-key \
9-
0x1eff08081394b72239a0cf7ff6b499213dcb7a338bedbd75d072d504588ef27a1f74d5ceb2f111ec02ede097fb09ed00aa9867922ed39299dae0b1afc0fa8661 \
10-
"This is a message that is signed""# ]
8+
crate::Command {
9+
[ Get the public key from a message and its signature => r#"forc crypto get-public-key \
10+
0x1eff08081394b72239a0cf7ff6b499213dcb7a338bedbd75d072d504588ef27a1f74d5ceb2f111ec02ede097fb09ed00aa9867922ed39299dae0b1afc0fa8661 \
11+
"This is a message that is signed""# ]
12+
}
1113
}
1214

1315
/// Parse a secret key to view the associated public key

forc-plugins/forc-crypto/src/keys/new_key.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ use std::ops::Deref;
1414
const ABOUT: &str = "Creates a new key for use with fuel-core";
1515

1616
forc_util::cli_examples! {
17-
[ Creates a new key default for block production => crypto "new-key" ]
18-
[ Creates a new key for peering => crypto "new-key -k peering" ]
19-
[ Creates a new key for block production => crypto "new-key -k block-production" ]
17+
crate::Command {
18+
[ Creates a new key default for block production => "forc crypto new-key" ]
19+
[ Creates a new key for peering => "forc crypto new-key -k peering" ]
20+
[ Creates a new key for block production => "forc crypto new-key -k block-production" ]
21+
}
2022
}
2123

2224
/// Generate a random new secret & public key in the format expected by fuel-core

forc-plugins/forc-crypto/src/keys/parse_secret.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use std::{ops::Deref, str::FromStr};
88
const ABOUT: &str = "Parses a private key to view the associated public key";
99

1010
forc_util::cli_examples! {
11-
[ Parses the secret of a block production => crypto "parse-secret \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
12-
[ Parses the secret of a peering => crypto "parse-secret -k peering \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
11+
crate::Command {
12+
[ Parses the secret of a block production => "forc crypto parse-secret \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
13+
[ Parses the secret of a peering => "forc crypto parse-secret -k peering \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
14+
}
1315
}
1416

1517
/// Parse a secret key to view the associated public key

forc-plugins/forc-doc/src/cli.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ use forc_pkg::source::IPFSNode;
55
const ABOUT: &str = "Forc plugin for building a Sway package's documentation";
66

77
forc_util::cli_examples! {
8-
[ Build the docs for a project in the current path => doc ""]
9-
[ Build the docs for a project in the current path and open it in the browser => doc "--open" ]
10-
[ Build the docs for a project located in another path => doc "--manifest-path ../tests_project2" ]
11-
[ Build the docs for the current project exporting private types => doc "--document-private-items" ]
12-
[ Build the docs offline without downloading any dependency from the network => doc "--offline" ]
8+
crate::Command {
9+
[ Build the docs for a project in the current path => "forc doc"]
10+
[ Build the docs for a project in the current path and open it in the browser => "forc doc --open" ]
11+
[ Build the docs for a project located in another path => "forc doc --manifest-path {path}" ]
12+
[ Build the docs for the current project exporting private types => "forc doc --document-private-items" ]
13+
[ Build the docs offline without downloading any dependency from the network => "forc doc --offline" ]
14+
}
1315
}
1416

1517
#[derive(Debug, Parser, Default)]

forc-plugins/forc-doc/tests/.gitignore

-2
This file was deleted.

forc-plugins/forc-doc/tests/Forc.lock

-13
This file was deleted.

forc-plugins/forc-doc/tests/Forc.toml

-8
This file was deleted.

forc-plugins/forc-doc/tests/src/main.sw

-11
This file was deleted.

forc-plugins/forc-fmt/src/main.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ use sway_utils::{constants, find_parent_manifest_dir, get_sway_files, is_sway_fi
1919
use swayfmt::Formatter;
2020

2121
forc_util::cli_examples! {
22-
[ Run the formatter in check mode on the current directory => fmt "--check"]
23-
[ Run the formatter in check mode on the current directory with short format => fmt "-c"]
24-
[ Run formatter against a given file => fmt "--file src/main.sw"]
25-
[ Run formatter against a given file with short format => fmt "-f src/main.sw"]
26-
[ Run formatter against a given dir => fmt "--path ../tests/"]
27-
[ Run formatter against a given dir with short format => fmt "-p ../tests"]
22+
crate::App {
23+
[ Run the formatter in check mode on the current directory => "forc fmt --check"]
24+
[ Run the formatter in check mode on the current directory with short format => "forc fmt -c"]
25+
[ Run formatter against a given file => "forc fmt --file {path}/src/main.sw"]
26+
[ Run formatter against a given file with short format => "forc fmt -f {path}/src/main.sw"]
27+
[ Run formatter against a given dir => "forc fmt --path {path}"]
28+
[ Run formatter against a given dir with short format => "forc fmt -p {path}"]
29+
}
2830
}
2931

3032
#[derive(Debug, Parser)]

forc-plugins/forc-fmt/tests/Forc.lock

-13
This file was deleted.

forc-plugins/forc-fmt/tests/Forc.toml

-8
This file was deleted.

forc-plugins/forc-fmt/tests/src/main.sw

-11
This file was deleted.

forc-plugins/forc-tx/src/lib.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ use std::path::PathBuf;
1313
use thiserror::Error;
1414

1515
forc_util::cli_examples! {
16-
[ Script example => tx r#"script --bytecode "out/debug/tests.bin" --data "data.bin" \
16+
{
17+
// This parser has a custom parser
18+
super::Command::try_parse_from_args
19+
} {
20+
[ Script example => r#"forc tx script --bytecode "{path}/out/debug/name.bin" --data "{path}/data.bin" \
1721
--receipts-root 0x2222222222222222222222222222222222222222222222222222222222222222"# ]
18-
[ Multiple inputs => tx r#"create --bytecode "out/debug/tests.bin"
19-
--storage-slots out/debug/tests-storage_slots.json
22+
[ Multiple inputs => r#"forc tx create --bytecode "{name}/out/debug/name.bin"
23+
--storage-slots "{path}/out/debug/name-storage_slots.json"
2024
--script-gas-limit 100 \
2125
--gas-price 0 \
2226
--maturity 0 \
@@ -59,9 +63,9 @@ forc_util::cli_examples! {
5963
--state-root 0x0000000000000000000000000000000000000000000000000000000000000000
6064
"#
6165
]
62-
[ An example constructing a create transaction => tx "create \
63-
--bytecode ./my-contract/out/debug/my-contract.bin \
64-
--storage-slots out/debug/tests-storage_slots.json
66+
[ An example constructing a create transaction => r#"forc tx create \
67+
--bytecode {path}/out/debug/name.bin \
68+
--storage-slots {path}/out/debug/name-storage_slots.json \
6569
--script-gas-limit 100 \
6670
--gas-price 0 \
6771
--maturity 0 \
@@ -88,9 +92,9 @@ forc_util::cli_examples! {
8892
--recipient 0x2222222222222222222222222222222222222222222222222222222222222222 \
8993
--amount 1 \
9094
--nonce 0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB \
91-
--msg-data ./message.dat \
92-
--predicate ./my-predicate2.bin \
93-
--predicate-data ./my-predicate2.dat \
95+
--msg-data {path}/message.dat \
96+
--predicate {path}/my-predicate2.bin \
97+
--predicate-data {path}/my-predicate2.dat \
9498
output coin \
9599
--to 0x2222222222222222222222222222222222222222222222222222222222222222 \
96100
--amount 100 \
@@ -109,8 +113,9 @@ forc_util::cli_examples! {
109113
--asset-id 0x0000000000000000000000000000000000000000000000000000000000000000 \
110114
output contract-created \
111115
--contract-id 0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC \
112-
--state-root 0x0000000000000000000000000000000000000000000000000000000000000000"
116+
--state-root 0x0000000000000000000000000000000000000000000000000000000000000000"#
113117
]
118+
}
114119
}
115120

116121
/// The top-level `forc tx` command.

0 commit comments

Comments
 (0)