-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Hooks | ||
|
||
Argc supports two kinds of hooks: | ||
|
||
- `_argc_before`: call before performing any operation | ||
- `_argc_before`: call after running the command function | ||
|
||
## Example | ||
|
||
```sh | ||
# @flag --foo | ||
# @option --bar | ||
|
||
_argc_before() { | ||
echo before | ||
} | ||
|
||
_argc_after() { | ||
echo after | ||
} | ||
|
||
main() { | ||
echo main | ||
} | ||
|
||
eval "$(argc --argc-eval "$0" "$@")" | ||
``` | ||
|
||
``` | ||
$ prog | ||
before | ||
main | ||
after | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#/usr/bin/env node | ||
|
||
set -e | ||
|
||
# @flag --foo | ||
# @option --bar | ||
|
||
_argc_before() { | ||
echo before | ||
} | ||
|
||
_argc_after() { | ||
echo after | ||
} | ||
|
||
main() { | ||
echo main | ||
} | ||
|
||
eval "$(argc --argc-eval "$0" "$@")" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::*; | ||
|
||
#[test] | ||
fn hook_without_subcmd() { | ||
let script = r###" | ||
_argc_before() { :; } | ||
_argc_after() { :; } | ||
"###; | ||
snapshot!(script, &["prog"]); | ||
} | ||
|
||
#[test] | ||
fn hook_with_main() { | ||
let script = r###" | ||
_argc_before() { :; } | ||
_argc_after() { :; } | ||
main() { :; } | ||
"###; | ||
snapshot!(script, &["prog"]); | ||
} | ||
|
||
#[test] | ||
fn hook_only_before() { | ||
let script = r###" | ||
_argc_before() { :; } | ||
main() { :; } | ||
"###; | ||
snapshot!(script, &["prog"]); | ||
} | ||
|
||
#[test] | ||
fn hook_with_subcmd() { | ||
let script = r###" | ||
_argc_before() { :; } | ||
_argc_after() { :; } | ||
# @cmd | ||
cmd() { :; } | ||
"###; | ||
snapshot!(script, &["prog", "cmd"]); | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/snapshots/integration__hook_fn__hook_only_before.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
source: tests/hook_fn.rs | ||
expression: data | ||
--- | ||
RUN | ||
prog | ||
|
||
OUTPUT | ||
argc__args=( prog ) | ||
argc__cmd_arg_index=0 | ||
argc__fn=main | ||
argc__positionals=( ) | ||
_argc_before | ||
main | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
source: tests/hook_fn.rs | ||
expression: data | ||
--- | ||
RUN | ||
prog | ||
|
||
OUTPUT | ||
argc__args=( prog ) | ||
argc__cmd_arg_index=0 | ||
argc__fn=main | ||
argc__positionals=( ) | ||
_argc_before | ||
main | ||
_argc_after | ||
|
16 changes: 16 additions & 0 deletions
16
tests/snapshots/integration__hook_fn__hook_with_subcmd.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
source: tests/hook_fn.rs | ||
expression: data | ||
--- | ||
RUN | ||
prog cmd | ||
|
||
OUTPUT | ||
argc__args=( prog cmd ) | ||
argc__cmd_arg_index=1 | ||
argc__fn=cmd | ||
argc__positionals=( ) | ||
_argc_before | ||
cmd | ||
_argc_after | ||
|
13 changes: 13 additions & 0 deletions
13
tests/snapshots/integration__hook_fn__hook_without_subcmd.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
source: tests/hook_fn.rs | ||
expression: data | ||
--- | ||
RUN | ||
prog | ||
|
||
OUTPUT | ||
argc__args=( prog ) | ||
argc__cmd_arg_index=0 | ||
argc__positionals=( ) | ||
_argc_before | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ mod compgen; | |
mod create; | ||
mod export; | ||
mod fail; | ||
mod hook_fn; | ||
mod main_fn; | ||
mod misc; | ||
mod parallel; | ||
|