Skip to content

Commit b855ddf

Browse files
lacolacoazu
andauthored
fix(usecase/nodecli): コマンドライン引数のデフォルト値を ?? でセットする (#1264)
* fix(usecase/nodecli): コマンドライン引数のデフォルト値を ?? でセットする * CI: Node.js 12.xをCIから外す * fix Co-authored-by: azu <azuciao@gmail.com>
1 parent 88464f1 commit b855ddf

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
runs-on: ubuntu-latest
2828
strategy:
2929
matrix:
30-
node-version: [16.x, 14.x, 12.x]
30+
node-version: [16.x, 14.x]
3131
name: "Test on Node.js ${{ matrix.node-version }}"
3232
steps:
3333
- uses: actions/checkout@v2

source/use-case/nodecli/md-to-html/README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,19 @@ console.log(options.gfm);
9898
### デフォルト設定を定義する {#declare-default}
9999

100100
アプリケーション側でデフォルト設定を持っておくことで、将来的にmarkedの挙動が変わったときにも影響を受けにくくなります。
101-
次のようにデフォルトのオプションを表現したオブジェクトに対して、`program.opts`メソッドの返り値で上書きしましょう。
102-
オブジェクトのデフォルト値を別のオブジェクトで上書きするときには`...`(spread構文)を使うと便利です(「[オブジェクト][]」の「[&#91;ES2018&#93; オブジェクトのspread構文でのマージ][]」を参照)。
101+
次のようにオプションを表現した`cliOptions`オブジェクトを作成し、`program.opts`メソッドの返り値から取得した値をセットします。
102+
コマンドライン引数で指定されなかったオプションには`??`[Nullish coalescing演算子][])を使ってデフォルトの値をセットします。
103+
Nullish coalescing演算子は左辺がnullishであるときにだけ右辺の値を返すため、値が指定されなかった状態と明示的に`false`が与えられた状態を区別したいときに便利です。
103104

104105
<!-- 差分コードなので -->
105106
<!-- doctest:disable -->
106107
```js
107-
// コマンドライン引数のオプションを取得し、デフォルトのオプションを上書きする
108+
// コマンドライン引数のオプションを取得する
109+
const options = program.opts();
110+
111+
// コマンドライン引数で指定されなかったオプションにデフォルト値を上書きする
108112
const cliOptions = {
109-
gfm: false,
110-
...program.opts(),
113+
gfm: options.gfm ?? false,
111114
};
112115
```
113116
@@ -154,5 +157,5 @@ $ node main.js --gfm sample.md
154157
[marked]: https://github.com/chjj/marked
155158
[変換オプション]: https://marked.js.org/#/USING_ADVANCED.md#options
156159
[GitHub Flavored Markdown]: https://github.github.com/gfm/
160+
[Nullish coalescing演算子]: ../../../basic/operator/README.md#nullish-coalescing-operator
157161
[オブジェクト]: ../../../basic/object/README.md
158-
[&#91;ES2018&#93; オブジェクトのspread構文でのマージ]: ../../../basic/object/README.md#object-spread-syntax

source/use-case/nodecli/md-to-html/src/main-3.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ program.option("--gfm", "GFMを有効にする");
77
program.parse(process.argv);
88
const filePath = program.args[0];
99

10-
// コマンドライン引数のオプションを取得し、デフォルトのオプションを上書きする
10+
// コマンドライン引数のオプションを取得する
11+
const options = program.opts();
12+
13+
// コマンドライン引数で指定されなかったオプションにデフォルト値を上書きする
1114
const cliOptions = {
12-
gfm: false,
13-
...program.opts(),
15+
gfm: options.gfm ?? false,
1416
};
1517

1618
fs.readFile(filePath, { encoding: "utf8" }, (err, file) => {

source/use-case/nodecli/md-to-html/src/main.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ program.option("--gfm", "GFMを有効にする");
77
program.parse(process.argv);
88
const filePath = program.args[0];
99

10-
// コマンドライン引数のオプションを取得し、デフォルトのオプションを上書きする
10+
// コマンドライン引数のオプションを取得する
11+
const options = program.opts();
12+
13+
// コマンドライン引数で指定されなかったオプションにデフォルト値を上書きする
1114
const cliOptions = {
12-
gfm: false,
13-
...program.opts(),
15+
gfm: options.gfm ?? false,
1416
};
1517

1618
fs.readFile(filePath, { encoding: "utf8" }, (err, file) => {

0 commit comments

Comments
 (0)