(argv)
| 36 | } |
| 37 | |
| 38 | function parseArgs(argv) { |
| 39 | const options = { |
| 40 | write: false, |
| 41 | check: false, |
| 42 | sidebar: DEFAULT_SIDEBAR, |
| 43 | route: null, |
| 44 | dir: null, |
| 45 | fallbackGroup: "其他", |
| 46 | verbose: false, |
| 47 | help: false, |
| 48 | }; |
| 49 | |
| 50 | for (let index = 0; index < argv.length; index += 1) { |
| 51 | const arg = argv[index]; |
| 52 | if (arg === "--help" || arg === "-h") { |
| 53 | options.help = true; |
| 54 | } else if (arg === "--write") { |
| 55 | options.write = true; |
| 56 | } else if (arg === "--check") { |
| 57 | options.check = true; |
| 58 | } else if (arg === "--verbose") { |
| 59 | options.verbose = true; |
| 60 | } else if (arg === "--sidebar") { |
| 61 | options.sidebar = path.resolve(ROOT_DIR, requireValue(argv, index)); |
| 62 | index += 1; |
| 63 | } else if (arg.startsWith("--sidebar=")) { |
| 64 | options.sidebar = path.resolve(ROOT_DIR, arg.slice("--sidebar=".length)); |
| 65 | } else if (arg === "--route") { |
| 66 | options.route = normalizeRoute(requireValue(argv, index)); |
| 67 | index += 1; |
| 68 | } else if (arg.startsWith("--route=")) { |
| 69 | options.route = normalizeRoute(arg.slice("--route=".length)); |
| 70 | } else if (arg === "--dir") { |
| 71 | options.dir = requireValue(argv, index); |
| 72 | index += 1; |
| 73 | } else if (arg.startsWith("--dir=")) { |
| 74 | options.dir = arg.slice("--dir=".length); |
| 75 | } else if (arg === "--fallback") { |
| 76 | options.fallbackGroup = requireValue(argv, index); |
| 77 | index += 1; |
| 78 | } else if (arg.startsWith("--fallback=")) { |
| 79 | options.fallbackGroup = arg.slice("--fallback=".length); |
| 80 | } else { |
| 81 | throw new Error(`Unknown option: ${arg}`); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if ((options.route && !options.dir) || (!options.route && options.dir)) { |
| 86 | throw new Error("--route and --dir must be used together"); |
| 87 | } |
| 88 | |
| 89 | return options; |
| 90 | } |
| 91 | |
| 92 | function requireValue(argv, index) { |
| 93 | const value = argv[index + 1]; |
no test coverage detected