@@ -188,6 +188,36 @@ assert_handler (const jerry_api_object_t *function_obj_p __attr_unused___, /** <
188188 }
189189} /* assert_handler */
190190
191+ static void
192+ print_usage (char * name )
193+ {
194+ printf ("Usage: %s [OPTION]... [FILE]...\n"
195+ "Try '%s --help' for more information.\n" ,
196+ name , name );
197+ } /* print_usage */
198+
199+ static void
200+ print_help (char * name )
201+ {
202+ printf ("Usage: %s [OPTION]... [FILE]...\n"
203+ "\n"
204+ "Options:\n"
205+ " -h, --help\n"
206+ " -v, --version\n"
207+ " --mem-stats\n"
208+ " --mem-stats-separate\n"
209+ " --parse-only\n"
210+ " --show-opcodes\n"
211+ " --save-snapshot-for-global FILE\n"
212+ " --save-snapshot-for-eval FILE\n"
213+ " --exec-snapshot FILE\n"
214+ " --log-level [0-3]\n"
215+ " --log-file FILE\n"
216+ " --abort-on-fail\n"
217+ "\n" ,
218+ name );
219+ } /* print_help */
220+
191221int
192222main (int argc ,
193223 char * * argv )
@@ -226,12 +256,18 @@ main (int argc,
226256#endif /* JERRY_ENABLE_LOG */
227257 for (i = 1 ; i < argc ; i ++ )
228258 {
229- if (!strcmp ("-v" , argv [i ]))
259+ if (!strcmp ("-h" , argv [i ]) || !strcmp ("--help" , argv [i ]))
260+ {
261+ print_help (argv [0 ]);
262+ return JERRY_STANDALONE_EXIT_CODE_OK ;
263+ }
264+ else if (!strcmp ("-v" , argv [i ]) || !strcmp ("--version" , argv [i ]))
230265 {
231266 printf ("Build date: \t%s\n" , jerry_build_date );
232267 printf ("Commit hash:\t%s\n" , jerry_commit_hash );
233268 printf ("Branch name:\t%s\n" , jerry_branch_name );
234269 printf ("\n" );
270+ return JERRY_STANDALONE_EXIT_CODE_OK ;
235271 }
236272 else if (!strcmp ("--mem-stats" , argv [i ]))
237273 {
@@ -255,66 +291,80 @@ main (int argc,
255291 is_save_snapshot_mode = true;
256292 is_save_snapshot_mode_for_global_or_eval = !strcmp ("--save-snapshot-for-global" , argv [i ]);
257293
258- flags |= JERRY_FLAG_PARSE_ONLY ;
259-
260- if (save_snapshot_file_name_p == NULL
261- && ++ i < argc )
294+ if (save_snapshot_file_name_p != NULL )
262295 {
263- save_snapshot_file_name_p = argv [i ];
296+ JERRY_ERROR_MSG ("Error: snapshot file name already specified\n" );
297+ print_usage (argv [0 ]);
298+ return JERRY_STANDALONE_EXIT_CODE_FAIL ;
264299 }
265- else
300+
301+ if (++ i >= argc )
266302 {
267- JERRY_ERROR_MSG ("Error: wrong format of the arguments\n" );
303+ JERRY_ERROR_MSG ("Error: no file specified for %s\n" , argv [i - 1 ]);
304+ print_usage (argv [0 ]);
268305 return JERRY_STANDALONE_EXIT_CODE_FAIL ;
269306 }
307+
308+ flags |= JERRY_FLAG_PARSE_ONLY ;
309+ save_snapshot_file_name_p = argv [i ];
270310 }
271311 else if (!strcmp ("--exec-snapshot" , argv [i ]))
272312 {
273- if (++ i < argc )
274- {
275- JERRY_ASSERT (exec_snapshots_count < JERRY_MAX_COMMAND_LINE_ARGS );
276- exec_snapshot_file_names [exec_snapshots_count ++ ] = argv [i ];
277- }
278- else
313+ if (++ i >= argc )
279314 {
280- JERRY_ERROR_MSG ("Error: wrong format of the arguments\n" );
315+ JERRY_ERROR_MSG ("Error: no file specified for %s\n" , argv [i - 1 ]);
316+ print_usage (argv [0 ]);
281317 return JERRY_STANDALONE_EXIT_CODE_FAIL ;
282318 }
319+
320+ JERRY_ASSERT (exec_snapshots_count < JERRY_MAX_COMMAND_LINE_ARGS );
321+ exec_snapshot_file_names [exec_snapshots_count ++ ] = argv [i ];
283322 }
284323 else if (!strcmp ("--log-level" , argv [i ]))
285324 {
286- flags |= JERRY_FLAG_ENABLE_LOG ;
287- if (++ i < argc && strlen (argv [i ]) == 1 && argv [i ][0 ] >='0' && argv [i ][0 ] <= '3' )
325+ if (++ i >= argc )
288326 {
289- #ifdef JERRY_ENABLE_LOG
290- jerry_debug_level = argv [i ][ 0 ] - '0' ;
291- #endif /* JERRY_ENABLE_LOG */
327+ JERRY_ERROR_MSG ( "Error: no level specified for %s\n" , argv [ i - 1 ]);
328+ print_usage ( argv [0 ]) ;
329+ return JERRY_STANDALONE_EXIT_CODE_FAIL ;
292330 }
293- else
331+
332+ if (strlen (argv [i ]) != 1 || argv [i ][0 ] < '0' || argv [i ][0 ] > '3' )
294333 {
295- JERRY_ERROR_MSG ("Error: wrong format or invalid argument\n" );
334+ JERRY_ERROR_MSG ("Error: wrong format for %s\n" , argv [i - 1 ]);
335+ print_usage (argv [0 ]);
296336 return JERRY_STANDALONE_EXIT_CODE_FAIL ;
297337 }
338+
339+ #ifdef JERRY_ENABLE_LOG
340+ flags |= JERRY_FLAG_ENABLE_LOG ;
341+ jerry_debug_level = argv [i ][0 ] - '0' ;
342+ #endif /* JERRY_ENABLE_LOG */
298343 }
299344 else if (!strcmp ("--log-file" , argv [i ]))
300345 {
301- flags |= JERRY_FLAG_ENABLE_LOG ;
302- if (++ i < argc )
303- {
304- #ifdef JERRY_ENABLE_LOG
305- log_file_name = argv [i ];
306- #endif /* JERRY_ENABLE_LOG */
307- }
308- else
346+ if (++ i >= argc )
309347 {
310- JERRY_ERROR_MSG ("Error: wrong format of the arguments\n" );
348+ JERRY_ERROR_MSG ("Error: no file specified for %s\n" , argv [i - 1 ]);
349+ print_usage (argv [0 ]);
311350 return JERRY_STANDALONE_EXIT_CODE_FAIL ;
312351 }
352+
353+ #ifdef JERRY_ENABLE_LOG
354+ flags |= JERRY_FLAG_ENABLE_LOG ;
355+ log_file_name = argv [i ];
356+ #endif /* JERRY_ENABLE_LOG */
313357 }
314358 else if (!strcmp ("--abort-on-fail" , argv [i ]))
315359 {
316360 flags |= JERRY_FLAG_ABORT_ON_FAIL ;
317361 }
362+ else if (!strncmp ("-" , argv [i ], 1 ))
363+ {
364+ JERRY_ERROR_MSG ("Error: unrecognized option: %s\n" , argv [i ]);
365+ print_usage (argv [0 ]);
366+ return JERRY_STANDALONE_EXIT_CODE_FAIL ;
367+ }
318368 else
319369 {
320370 file_names [files_counter ++ ] = argv [i ];
0 commit comments