@@ -42,22 +42,40 @@ listeners.forEach(listener => process.removeListener("warning", listener));
42
42
43
43
const inputStrings = process . argv . slice ( 2 ) ;
44
44
45
- const userWantsGeneralHelp =
46
- inputStrings . length === 0 ||
47
- ( inputStrings . length === 1 && [ "help" , "--help" ] . includes ( inputStrings [ 0 ] ) ) ;
48
-
49
- if ( userWantsGeneralHelp ) {
50
- const { displayGeneralHelp } = require ( "./lib/command-utils" ) ;
51
- displayGeneralHelp ( ) ;
52
- process . exit ( 0 ) ;
53
- }
54
-
55
45
const {
56
46
getCommand,
57
47
prepareOptions,
58
- runCommand
48
+ runCommand,
49
+ displayGeneralHelp
59
50
} = require ( "./lib/command-utils" ) ;
60
51
52
+ //User only enter truffle with no commands, let's show them what's available.
53
+ if ( inputStrings . length === 0 ) {
54
+ displayGeneralHelp ( ) ;
55
+ process . exit ( ) ;
56
+ }
57
+
58
+ //if `help` or `--help` is in the command, validate and transform the input argument for help
59
+ if (
60
+ inputStrings . some ( inputString => [ "help" , "--help" ] . includes ( inputString ) )
61
+ ) {
62
+ //when user wants general help
63
+ if ( inputStrings . length === 1 ) {
64
+ displayGeneralHelp ( ) ;
65
+ process . exit ( ) ;
66
+ }
67
+
68
+ //check where is --help used, mutate argument into a proper help command
69
+ const helpIndex = inputStrings . indexOf ( "--help" ) ;
70
+
71
+ if ( helpIndex !== - 1 ) {
72
+ //remove `--help` from array
73
+ inputStrings . splice ( helpIndex , 1 ) ;
74
+ //insert `help` in first position
75
+ inputStrings . unshift ( "help" ) ;
76
+ }
77
+ }
78
+
61
79
const command = getCommand ( {
62
80
inputStrings,
63
81
options : { } ,
@@ -67,7 +85,9 @@ const command = getCommand({
67
85
//getCommand() will return null if a command not recognized by truffle is used.
68
86
if ( command === null ) {
69
87
console . log (
70
- `\`truffle ${ inputStrings } \` is not a valid truffle command. Please see \`truffle help\` for available commands.`
88
+ `\`truffle ${ inputStrings . join (
89
+ " "
90
+ ) } \` is not a valid truffle command. Please see \`truffle help\` for available commands.`
71
91
) ;
72
92
process . exit ( 1 ) ;
73
93
}
0 commit comments