-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feature: implement a custom argparse with tests
- Loading branch information
1 parent
e51e7a6
commit 9d6e735
Showing
5 changed files
with
69 additions
and
5 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
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,5 @@ | ||
package main | ||
|
||
func main(){ | ||
println("Hello World!") | ||
} |
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,19 @@ | ||
package argparse | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
) | ||
|
||
/* | ||
Parse the user input given a `flag.FlagSet` to be easy testing | ||
*/ | ||
func ParseArgs(fs *flag.FlagSet) Args{ | ||
arguments := Args{} | ||
|
||
fs.BoolVar(&arguments.DebugMode, "debug" , false, "this function enable debug") | ||
|
||
fs.Parse(os.Args[1:]) | ||
|
||
return arguments | ||
} |
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,5 @@ | ||
package argparse | ||
|
||
type Args struct { | ||
DebugMode bool | ||
} |
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,35 @@ | ||
package argparse_test | ||
|
||
import ( | ||
"Animatic-v2/internal/argparse" | ||
"flag" | ||
"os" | ||
"testing" | ||
|
||
) | ||
|
||
func TestArgparse(t *testing.T){ | ||
t.Run("Should return the struct if debug setted to false", func(t *testing.T) { | ||
fs := flag.NewFlagSet("test", flag.ContinueOnError) | ||
|
||
os.Args = []string{"cmd", "-debug=false"} | ||
|
||
argv := argparse.ParseArgs(fs) | ||
|
||
if argv.DebugMode != false { | ||
t.Fatalf("expected %v instead %v\n", !argv.DebugMode, argv.DebugMode) | ||
} | ||
}) | ||
|
||
t.Run("Should return the struct if debug setted to true", func(t *testing.T) { | ||
fs := flag.NewFlagSet("test", flag.ContinueOnError) | ||
|
||
os.Args = []string{"cmd", "-debug"} | ||
|
||
argv := argparse.ParseArgs(fs) | ||
|
||
if argv.DebugMode != true { | ||
t.Fatalf("expected %v instead %v\n", argv.DebugMode, !argv.DebugMode) | ||
} | ||
}) | ||
} |