Skip to content

Commit adef04b

Browse files
authored
Removing dependency on args being in alphabetical order. (#507)
* Removing dependency on args being in alphabetical order. Previously, arg parser would display the arguments in the help section in alphabetical order. Since we're now moving in the direction of grouping arguments by functionality independent of names (like for upcoming --gitignore flag), we now have to come up with a quick refactoring that allows arguments to be displayed in a specified order regardless of what alphabetical order the arguments themselves occupy. * Changing sorting code per PR comment
1 parent af38d01 commit adef04b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

bin/jscodeshift.js

+22
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,23 @@ const pkg = require('../package.json');
1717
const parser = require('../src/argsParser')
1818
.options({
1919
transform: {
20+
display_index: 15,
2021
abbr: 't',
2122
default: './transform.js',
2223
help: 'path to the transform file. Can be either a local path or url',
2324
metavar: 'FILE',
2425
required: true
2526
},
2627
cpus: {
28+
display_index: 1,
2729
abbr: 'c',
2830
help: 'start at most N child processes to process source files',
2931
defaultHelp: 'max(all - 1, 1)',
3032
metavar: 'N',
3133
process: Number,
3234
},
3335
verbose: {
36+
display_index: 16,
3437
abbr: 'v',
3538
choices: [0, 1, 2],
3639
default: 0,
@@ -39,69 +42,87 @@ const parser = require('../src/argsParser')
3942
process: Number,
4043
},
4144
dry: {
45+
display_index: 2,
4246
abbr: 'd',
4347
flag: true,
4448
default: false,
4549
help: 'dry run (no changes are made to files)'
4650
},
4751
print: {
52+
display_index: 11,
4853
abbr: 'p',
4954
flag: true,
5055
default: false,
5156
help: 'print transformed files to stdout, useful for development'
5257
},
5358
babel: {
59+
display_index: 0,
5460
flag: true,
5561
default: true,
5662
help: 'apply babeljs to the transform file'
5763
},
5864
extensions: {
65+
display_index: 3,
5966
default: 'js',
6067
help: 'transform files with these file extensions (comma separated list)',
6168
metavar: 'EXT',
6269
},
6370
ignorePattern: {
71+
display_index: 7,
6472
full: 'ignore-pattern',
6573
list: true,
6674
help: 'ignore files that match a provided glob expression',
6775
metavar: 'GLOB',
6876
},
6977
ignoreConfig: {
78+
display_index: 6,
7079
full: 'ignore-config',
7180
list: true,
7281
help: 'ignore files if they match patterns sourced from a configuration file (e.g. a .gitignore)',
7382
metavar: 'FILE'
7483
},
84+
gitignore: {
85+
display_index: 8,
86+
flag: true,
87+
default: false,
88+
help: 'adds entries the current directory\'s .gitignore file',
89+
},
7590
runInBand: {
91+
display_index: 12,
7692
flag: true,
7793
default: false,
7894
full: 'run-in-band',
7995
help: 'run serially in the current process'
8096
},
8197
silent: {
98+
display_index: 13,
8299
abbr: 's',
83100
flag: true,
84101
default: false,
85102
help: 'do not write to stdout or stderr'
86103
},
87104
parser: {
105+
display_index: 9,
88106
choices: ['babel', 'babylon', 'flow', 'ts', 'tsx'],
89107
default: 'babel',
90108
help: 'the parser to use for parsing the source files'
91109
},
92110
parserConfig: {
111+
display_index: 10,
93112
full: 'parser-config',
94113
help: 'path to a JSON file containing a custom parser configuration for flow or babylon',
95114
metavar: 'FILE',
96115
process: file => JSON.parse(fs.readFileSync(file)),
97116
},
98117
failOnError: {
118+
display_index: 4,
99119
flag: true,
100120
help: 'Return a non-zero code when there are errors',
101121
full: 'fail-on-error',
102122
default: false,
103123
},
104124
version: {
125+
display_index: 17,
105126
help: 'print version and exit',
106127
callback: function() {
107128
const requirePackage = require('../utils/requirePackage');
@@ -115,6 +136,7 @@ const parser = require('../src/argsParser')
115136
},
116137
},
117138
stdin: {
139+
display_index: 14,
118140
help: 'read file/directory list from stdin',
119141
flag: true,
120142
default: false,

src/argsParser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function formatOption(option) {
4848
function getHelpText(options) {
4949
const opts = Object.keys(options)
5050
.map(k => options[k])
51-
.sort((a,b) => a.full.localeCompare(b.full));
51+
.sort((a,b) => a.display_index - b.display_index);
5252

5353
const text = `
5454
Usage: jscodeshift [OPTION]... PATH...
@@ -89,6 +89,7 @@ function validateOptions(parsedOptions, options) {
8989

9090
function prepareOptions(options) {
9191
options.help = {
92+
display_index: 5,
9293
abbr: 'h',
9394
help: 'print this help and exit',
9495
callback() {

0 commit comments

Comments
 (0)