1+ // tslint:disable: no-console
12import * as commandLineArgs from "command-line-args" ;
23import * as semver from "semver" ;
34import { isReleaseType } from "../release-type" ;
45import { VersionBumpOptions } from "../version-bump-options" ;
6+ import { ExitCode } from "./exit-code" ;
7+ import { helpText } from "./help" ;
58
69/**
710 * The parsed command-line arguments
@@ -17,62 +20,70 @@ export interface ParsedArgs {
1720 * Parses the command-line arguments
1821 */
1922export function parseArgs ( argv : string [ ] ) : ParsedArgs {
20- let args = commandLineArgs (
21- [
22- { name : "preid" , type : String } ,
23- { name : "commit" , alias : "c" , type : String } ,
24- { name : "tag" , alias : "t" , type : String } ,
25- { name : "push" , alias : "p" , type : Boolean } ,
26- { name : "all" , alias : "a" , type : Boolean } ,
27- { name : "no-verify" , type : Boolean } ,
28- { name : "quiet" , alias : "q" , type : Boolean } ,
29- { name : "version" , alias : "v" , type : Boolean } ,
30- { name : "help" , alias : "h" , type : Boolean } ,
31- { name : "files" , type : String , multiple : true , defaultOption : true } ,
32- ] ,
33- { argv }
34- ) ;
23+ try {
24+ let args = commandLineArgs (
25+ [
26+ { name : "preid" , type : String } ,
27+ { name : "commit" , alias : "c" , type : String } ,
28+ { name : "tag" , alias : "t" , type : String } ,
29+ { name : "push" , alias : "p" , type : Boolean } ,
30+ { name : "all" , alias : "a" , type : Boolean } ,
31+ { name : "no-verify" , type : Boolean } ,
32+ { name : "quiet" , alias : "q" , type : Boolean } ,
33+ { name : "version" , alias : "v" , type : Boolean } ,
34+ { name : "help" , alias : "h" , type : Boolean } ,
35+ { name : "files" , type : String , multiple : true , defaultOption : true } ,
36+ ] ,
37+ { argv }
38+ ) ;
3539
36- let parsedArgs : ParsedArgs = {
37- help : args . help as boolean ,
38- version : args . version as boolean ,
39- quiet : args . quiet as boolean ,
40- options : {
41- preid : args . preid as string ,
42- commit : args . commit as string | boolean ,
43- tag : args . tag as string | boolean ,
44- push : args . push as boolean ,
45- all : args . all as boolean ,
46- noVerify : args [ "no-verify" ] as boolean ,
47- files : args . files as string [ ] ,
48- }
49- } ;
40+ let parsedArgs : ParsedArgs = {
41+ help : args . help as boolean ,
42+ version : args . version as boolean ,
43+ quiet : args . quiet as boolean ,
44+ options : {
45+ preid : args . preid as string ,
46+ commit : args . commit as string | boolean ,
47+ tag : args . tag as string | boolean ,
48+ push : args . push as boolean ,
49+ all : args . all as boolean ,
50+ noVerify : args [ "no-verify" ] as boolean ,
51+ files : args . files as string [ ] ,
52+ }
53+ } ;
5054
51- // If --preid is used without an argument, then throw an error, since it's probably a mistake.
52- // If they want to use the default value ("beta"), then they should not pass the argument at all
53- if ( args . preid === null ) {
54- throw new Error ( `The --preid option requires a value, such as "alpha", "beta", etc.` ) ;
55- }
55+ // If --preid is used without an argument, then throw an error, since it's probably a mistake.
56+ // If they want to use the default value ("beta"), then they should not pass the argument at all
57+ if ( args . preid === null ) {
58+ throw new Error ( `The --preid option requires a value, such as "alpha", "beta", etc.` ) ;
59+ }
5660
57- // If --commit is used without an argument, then treat it as a boolean flag
58- if ( args . commit === null ) {
59- parsedArgs . options . commit = true ;
60- }
61+ // If --commit is used without an argument, then treat it as a boolean flag
62+ if ( args . commit === null ) {
63+ parsedArgs . options . commit = true ;
64+ }
6165
62- // If --tag is used without an argument, then treat it as a boolean flag
63- if ( args . tag === null ) {
64- parsedArgs . options . tag = true ;
65- }
66+ // If --tag is used without an argument, then treat it as a boolean flag
67+ if ( args . tag === null ) {
68+ parsedArgs . options . tag = true ;
69+ }
6670
67- // If a version number or release type was specified, then it will mistakenly be added to the "files" array
68- if ( parsedArgs . options . files && parsedArgs . options . files . length > 0 ) {
69- let firstArg = parsedArgs . options . files [ 0 ] ;
71+ // If a version number or release type was specified, then it will mistakenly be added to the "files" array
72+ if ( parsedArgs . options . files && parsedArgs . options . files . length > 0 ) {
73+ let firstArg = parsedArgs . options . files [ 0 ] ;
7074
71- if ( firstArg === "prompt" || isReleaseType ( firstArg ) || semver . valid ( firstArg ) ) {
72- parsedArgs . options . release = firstArg ;
73- parsedArgs . options . files . shift ( ) ;
75+ if ( firstArg === "prompt" || isReleaseType ( firstArg ) || semver . valid ( firstArg ) ) {
76+ parsedArgs . options . release = firstArg ;
77+ parsedArgs . options . files . shift ( ) ;
78+ }
7479 }
75- }
7680
77- return parsedArgs ;
81+ return parsedArgs ;
82+ }
83+ catch ( error ) {
84+ // There was an error parsing the command-line args
85+ console . error ( ( error as Error ) . message ) ;
86+ console . error ( helpText ) ;
87+ return process . exit ( ExitCode . InvalidArgument ) ;
88+ }
7889}
0 commit comments