@@ -1014,6 +1014,86 @@ Otherwise, returns `false`.
1014
1014
See [ ` assert.deepStrictEqual() ` ] [ ] for more information about deep strict
1015
1015
equality.
1016
1016
1017
+ ## ` util.parseArgs([config]) `
1018
+
1019
+ <!-- YAML
1020
+ added: REPLACEME
1021
+ -->
1022
+
1023
+ > Stability: 1 - Experimental
1024
+
1025
+ * ` config ` {Object} Used to provide arguments for parsing and to configure
1026
+ the parser. ` config ` supports the following properties:
1027
+ * ` args ` {string\[ ] } array of argument strings. ** Default:** ` process.argv `
1028
+ with ` execPath ` and ` filename ` removed.
1029
+ * ` options ` {Object} Used to describe arguments known to the parser.
1030
+ Keys of ` options ` are the long names of options and values are an
1031
+ {Object} accepting the following properties:
1032
+ * ` type ` {string} Type of argument, which must be either ` boolean ` or ` string ` .
1033
+ * ` multiple ` {boolean} Whether this option can be provided multiple
1034
+ times. If ` true ` , all values will be collected in an array. If
1035
+ ` false ` , values for the option are last-wins. ** Default:** ` false ` .
1036
+ * ` short ` {string} A single character alias for the option.
1037
+ * ` strict ` : {boolean} Should an error be thrown when unknown arguments
1038
+ are encountered, or when arguments are passed that do not match the
1039
+ ` type ` configured in ` options ` .
1040
+ ** Default:** ` true ` .
1041
+ * ` allowPositionals ` : {boolean} Whether this command accepts positional
1042
+ arguments.
1043
+ ** Default:** ` false ` if ` strict ` is ` true ` , otherwise ` true ` .
1044
+
1045
+ * Returns: {Object} The parsed command line arguments:
1046
+ * ` values ` {Object} A mapping of parsed option names with their {string}
1047
+ or {boolean} values.
1048
+ * ` positionals ` {string\[ ] } Positional arguments.
1049
+
1050
+ Provides a higher level API for command-line argument parsing than interacting
1051
+ with ` process.argv ` directly. Takes a specification for the expected arguments
1052
+ and returns a structured object with the parsed options and positionals.
1053
+
1054
+ ``` mjs
1055
+ import { parseArgs } from ' node:util' ;
1056
+ const args = [' -f' , ' --bar' , ' b' ];
1057
+ const options = {
1058
+ foo: {
1059
+ type: ' boolean' ,
1060
+ short: ' f'
1061
+ },
1062
+ bar: {
1063
+ type: ' string'
1064
+ }
1065
+ };
1066
+ const {
1067
+ values ,
1068
+ positionals
1069
+ } = parseArgs ({ args, options });
1070
+ console .log (values, positionals);
1071
+ // Prints: [Object: null prototype] { foo: true, bar: 'b' } []
1072
+ ```
1073
+
1074
+ ``` cjs
1075
+ const { parseArgs } = require (' node:util' );
1076
+ const args = [' -f' , ' --bar' , ' b' ];
1077
+ const options = {
1078
+ foo: {
1079
+ type: ' boolean' ,
1080
+ short: ' f'
1081
+ },
1082
+ bar: {
1083
+ type: ' string'
1084
+ }
1085
+ };
1086
+ const {
1087
+ values ,
1088
+ positionals
1089
+ } = parseArgs ({ args, options });
1090
+ console .log (values, positionals);
1091
+ // Prints: [Object: null prototype] { foo: true, bar: 'b' } []ss
1092
+ ```
1093
+
1094
+ ` util.parseArgs ` is experimental and behavior may change. Join the
1095
+ conversation in [ pkgjs/parseargs] [ ] to contribute to the design.
1096
+
1017
1097
## ` util.promisify(original) `
1018
1098
1019
1099
<!-- YAML
@@ -2685,5 +2765,6 @@ util.log('Timestamped message.');
2685
2765
[ default sort ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
2686
2766
[ global symbol registry ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
2687
2767
[ list of deprecated APIS ] : deprecations.md#list-of-deprecated-apis
2768
+ [ pkgjs/parseargs ] : https://github.com/pkgjs/parseargs
2688
2769
[ semantically incompatible ] : https://github.com/nodejs/node/issues/4179
2689
2770
[ util.inspect.custom ] : #utilinspectcustom
0 commit comments