edinet_cli 0.0.4

A command-line interface for EDINET.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// SPDX-FileCopyrightText: 2026 Strike Group Co., Ltd.
//
// SPDX-License-Identifier: Apache-2.0

mod app_config;
mod app_paths;
mod clear;
mod document_id;
mod downloader;
mod getter;
mod searcher;
mod status;
mod store;
mod submission_year;
mod updater;
mod zip_archive;

use clap::{Command, CommandFactory, FromArgMatches, Parser};

const HELP_TEMPLATE: &str =
    "{before-help}{about-with-newline}\n使い方: {usage}\n\n{all-args}{after-help}";

#[derive(Parser, Debug)]
#[command(
    name = "edinet",
    version = env!("CARGO_PKG_VERSION"),
    about = "EDINET の有価証券報告書を取得・解析するCLIツール",
    arg_required_else_help = true,
    after_help = "使用例:\n  \
                  edinet setup --key YOUR_API_KEY\n  \
                  edinet update\n  \
                  edinet search ストライク --limit 3\n  \
                  edinet get --company ストライク\n  \
                  edinet download pdf ./downloads --company ストライク\n\n\
                  各コマンドの詳細は `edinet <COMMAND> --help` で確認できます。"
)]
struct Cli {
    #[command(subcommand)]
    subcommand: SubCommand,
}

#[derive(clap::Subcommand, Debug)]
enum SubCommand {
    #[command(
        about = "EDINET API の API キーを登録します",
        after_help = "使用例:\n  edinet setup --key YOUR_API_KEY",
        visible_alias = "init"
    )]
    Setup(crate::app_config::SetupArgs),
    #[command(
        about = "検索インデックスを EDINET から更新します",
        after_help = "使用例:\n  \
                      edinet update\n  \
                      edinet update --years 3 --concurrency 8\n  \
                      edinet update --years 3 --sequential\n  \
                      edinet update --today\n  \
                      edinet update --from 2026-04-01 --to 2026-04-30",
        visible_alias = "u"
    )]
    Update(crate::updater::command::UpdateArgs),
    #[command(
        about = "有価証券報告書を検索します",
        after_help = "使用例:\n  \
                      edinet search ストライク\n  \
                      edinet search --sec-code 7203\n  \
                      edinet search --company ストライク --year 2025\n  \
                      edinet search トヨタ --from 2026-04-01 --to 2026-04-30 --limit 20\n  \
                      edinet search --company ストライク --json",
        visible_alias = "s"
    )]
    Search(crate::searcher::command::SearchArgs),
    #[command(
        about = "有価証券報告書を取得し、内容を項目別に出力します",
        after_help = "使用例:\n  \
                      edinet get --company ストライク\n  \
                      edinet get --edinet-code E32380 --lang ja\n  \
                      edinet get --company ストライク --year 2025\n  \
                      edinet get --doc-id S100XAN1\n  \
                      edinet get --company ストライク --offline",
        visible_alias = "g"
    )]
    Get(crate::getter::command::GetArgs),
    #[command(
        about = "有価証券報告書を PDF などの形式でダウンロードします",
        after_help = "使用例:\n  \
                      edinet download pdf ./downloads --company ストライク\n  \
                      edinet download csv ./downloads --doc-id S100XAN1 --extract\n  \
                      edinet download pdf ./downloads --company ストライク --year 2025\n  \
                      edinet download xbrl ./downloads --edinet-code E32380",
        visible_alias = "d"
    )]
    Download(crate::downloader::command::DownloadArgs),
    #[command(
        about = "検索インデックスとダウンロード済み CSV キャッシュを削除します",
        after_help = "使用例:\n  edinet clear",
        visible_alias = "c"
    )]
    Clear,
    #[command(
        about = "各種状態を表示します",
        after_help = "使用例:\n  edinet status",
        visible_alias = "st"
    )]
    Status,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let _ = dotenvy::dotenv();
    let matches = cli_command().get_matches();
    let cli = Cli::from_arg_matches(&matches)?;

    match cli.subcommand {
        SubCommand::Setup(command) => crate::app_config::run_setup(command)?,
        SubCommand::Update(command) => crate::updater::command::run(command).await?,
        SubCommand::Search(command) => crate::searcher::command::run(command).await?,
        SubCommand::Get(command) => crate::getter::command::run(command).await?,
        SubCommand::Download(command) => crate::downloader::command::run(command).await?,
        SubCommand::Clear => {
            crate::clear::clear_local_data()?;
        }
        SubCommand::Status => {
            crate::status::run().await?;
        }
    }

    Ok(())
}

fn cli_command() -> Command {
    let mut command = Cli::command();
    command.build();
    localize_help(command)
}

fn localize_help(command: Command) -> Command {
    command
        .help_template(HELP_TEMPLATE)
        .subcommand_help_heading("コマンド")
        .mut_args(|arg| {
            let heading = if arg.get_index().is_some() {
                "引数"
            } else {
                "オプション"
            };
            let arg = arg.help_heading(heading);

            match arg.get_id().as_str() {
                "help" => arg.help("ヘルプを表示します"),
                "version" => arg.help("バージョンを表示します"),
                _ => arg,
            }
        })
        .mut_subcommands(|subcommand| {
            let subcommand = if subcommand.get_name() == "help" {
                subcommand.about("指定したコマンドのヘルプを表示します")
            } else {
                subcommand
            };
            localize_help(subcommand)
        })
}

#[cfg(test)]
mod tests {
    use super::cli_command;

    fn render_help(path: &[&str]) -> String {
        let mut command = cli_command();
        let mut current = &mut command;

        for name in path {
            current = current
                .find_subcommand_mut(name)
                .unwrap_or_else(|| panic!("subcommand `{name}` must exist"));
        }

        let mut output = Vec::new();
        current
            .write_help(&mut output)
            .expect("help must be renderable");
        String::from_utf8(output).expect("help must be UTF-8")
    }

    fn help_output(args: &[&str]) -> String {
        cli_command()
            .try_get_matches_from(args)
            .expect_err("help flag must stop argument parsing")
            .to_string()
    }

    #[test]
    fn document_commands_reject_unsafe_document_ids() {
        for args in [
            &["edinet", "get", "--doc-id", "../file1"][..],
            &[
                "edinet",
                "download",
                "csv",
                "./downloads",
                "--doc-id",
                "S100/AN1",
            ][..],
        ] {
            let error = cli_command()
                .try_get_matches_from(args)
                .expect_err("unsafe document ID must be rejected");
            assert!(
                error
                    .to_string()
                    .contains("document ID must be exactly 8 uppercase ASCII letters or digits")
            );
        }
    }

    #[test]
    fn root_help_guides_initial_setup_and_command_discovery() {
        let help = render_help(&[]);

        assert!(help.contains("edinet setup --key YOUR_API_KEY"));
        assert!(help.contains("edinet update"));
        assert!(help.contains("edinet <COMMAND> --help"));
        assert!(help.contains("EDINET の有価証券報告書を取得・解析するCLIツール"));
        assert!(help.contains("使い方: edinet <COMMAND>"));
        assert!(help.contains("コマンド:"));
        assert!(help.contains("オプション:"));
        for alias in [
            "[aliases: init]",
            "[aliases: u]",
            "[aliases: s]",
            "[aliases: g]",
            "[aliases: d]",
            "[aliases: c]",
            "[aliases: st]",
        ] {
            assert!(help.contains(alias), "root help must contain `{alias}`");
        }
        assert!(!help.contains("Print help"));
    }

    #[test]
    fn every_command_has_a_usage_example() {
        for (path, example) in [
            (&["setup"][..], "edinet setup --key YOUR_API_KEY"),
            (&["update"][..], "edinet update --years 3"),
            (&["search"][..], "edinet search --sec-code 7203"),
            (&["get"][..], "edinet get --company ストライク"),
            (
                &["download"][..],
                "edinet download csv ./downloads --doc-id S100XAN1 --extract",
            ),
            (&["clear"][..], "edinet clear"),
            (&["status"][..], "edinet status"),
        ] {
            let help = render_help(path);
            assert!(
                help.contains(example),
                "help for `{}` must contain `{example}`",
                path.join(" ")
            );
        }
    }

    #[test]
    fn destructive_and_sensitive_commands_explain_their_scope() {
        assert!(render_help(&["clear"]).contains("CSV キャッシュを削除します"));
        assert!(render_help(&["status"]).contains("各種状態を表示します"));
    }

    #[test]
    fn update_years_requires_positive_value_and_excludes_other_periods() {
        assert!(
            cli_command()
                .try_get_matches_from(["edinet", "update", "--years", "0"])
                .is_err()
        );
        assert!(
            cli_command()
                .try_get_matches_from(["edinet", "update", "--concurrency", "0"])
                .is_err()
        );
        assert!(
            cli_command()
                .try_get_matches_from(["edinet", "update", "--sequential", "--concurrency", "2",])
                .is_err()
        );
        assert!(
            cli_command()
                .try_get_matches_from(["edinet", "update", "--years", "2", "--today"])
                .is_err()
        );
        assert!(
            cli_command()
                .try_get_matches_from([
                    "edinet",
                    "update",
                    "--years",
                    "2",
                    "--from",
                    "2025-01-01",
                    "--to",
                    "2025-12-31",
                ])
                .is_err()
        );
    }

    #[test]
    fn update_force_is_documented() {
        let help = render_help(&["update"]);

        assert!(help.contains("-F, --force"));
        assert!(help.contains("更新済み日付を無視して対象期間をすべて再取得します"));
    }

    #[test]
    fn year_filter_is_available_on_document_commands() {
        for (path, example) in [
            (
                &["search"][..],
                "edinet search --company ストライク --year 2025",
            ),
            (&["get"][..], "edinet get --company ストライク --year 2025"),
            (
                &["download"][..],
                "edinet download pdf ./downloads --company ストライク --year 2025",
            ),
        ] {
            let help = render_help(path);
            assert!(help.contains("-y, --year <YEAR>"));
            assert!(help.contains(example));
        }

        for args in [
            &["edinet", "search", "-y", "2025"][..],
            &["edinet", "get", "-c", "ストライク", "-y", "2025"][..],
            &[
                "edinet",
                "download",
                "pdf",
                "-c",
                "ストライク",
                "-y",
                "2025",
            ][..],
        ] {
            cli_command()
                .try_get_matches_from(args)
                .unwrap_or_else(|error| panic!("year filter must parse: {error}"));
        }
    }

    #[test]
    fn year_filter_rejects_invalid_or_conflicting_values() {
        for args in [
            &["edinet", "search", "--year", "25"][..],
            &["edinet", "search", "--year", "2025", "--date", "2025-01-01"][..],
            &["edinet", "get", "--doc-id", "S100TEST", "--year", "2025"][..],
        ] {
            assert!(cli_command().try_get_matches_from(args).is_err());
        }
    }

    #[test]
    fn get_and_download_reject_removed_date_filters() {
        for args in [
            &["edinet", "get", "--date", "2025-01-01"][..],
            &["edinet", "get", "--from", "2025-01-01"][..],
            &["edinet", "get", "--to", "2025-12-31"][..],
            &["edinet", "download", "pdf", "--date", "2025-01-01"][..],
            &["edinet", "download", "pdf", "--from", "2025-01-01"][..],
            &["edinet", "download", "pdf", "--to", "2025-12-31"][..],
        ] {
            assert!(cli_command().try_get_matches_from(args).is_err());
        }
    }

    #[test]
    fn get_uses_lang_option_for_json_key_language() {
        let help = render_help(&["get"]);
        assert!(help.contains("-l, --lang <LANG>"));
        assert!(!help.contains("--keys"));

        assert!(
            cli_command()
                .try_get_matches_from(["edinet", "get", "--company", "ストライク", "--lang", "ja"])
                .is_ok()
        );
        assert!(
            cli_command()
                .try_get_matches_from(["edinet", "get", "--company", "ストライク", "--keys", "ja"])
                .is_err()
        );
    }

    #[test]
    fn command_aliases_are_accepted() {
        for args in [
            &["edinet", "init", "-h"][..],
            &["edinet", "u", "-h"][..],
            &["edinet", "s", "-h"][..],
            &["edinet", "g", "-h"][..],
            &["edinet", "d", "-h"][..],
            &["edinet", "c", "-h"][..],
            &["edinet", "st", "-h"][..],
        ] {
            cli_command()
                .try_get_matches_from(args)
                .expect_err("help flag for command alias must stop argument parsing");
        }
    }

    #[test]
    fn option_aliases_are_accepted() {
        for args in [
            &["edinet", "init", "-k", "API_KEY"][..],
            &[
                "edinet",
                "u",
                "-f",
                "2025-01-01",
                "-t",
                "2025-12-31",
                "-k",
                "API_KEY",
            ][..],
            &["edinet", "u", "-y", "2", "-p", "8", "-k", "API_KEY"][..],
            &["edinet", "u", "-y", "2", "-s", "-F", "-k", "API_KEY"][..],
            &["edinet", "u", "-d", "-k", "API_KEY"][..],
            &[
                "edinet",
                "s",
                "query",
                "-e",
                "E02144",
                "-s",
                "7203",
                "-c",
                "トヨタ",
                "-n",
                "JCN",
                "-d",
                "2025-01-01",
                "-l",
                "10",
                "-p",
                "2",
                "-o",
                "submit-date-asc",
                "-j",
            ][..],
            &["edinet", "s", "-f", "2025-01-01", "-t", "2025-12-31"][..],
            &[
                "edinet",
                "g",
                "-e",
                "E02144",
                "-c",
                "トヨタ",
                "-y",
                "2025",
                "-o",
            ][..],
            &["edinet", "g", "-i", "S100TEST", "-k", "API_KEY", "-l", "ja"][..],
            &[
                "edinet",
                "d",
                "csv",
                "-e",
                "E02144",
                "-c",
                "トヨタ",
                "-y",
                "2025",
                "-x",
            ][..],
            &["edinet", "d", "pdf", "-i", "S100TEST", "-k", "API_KEY"][..],
        ] {
            cli_command()
                .try_get_matches_from(args)
                .unwrap_or_else(|error| panic!("short options must parse: {error}"));
        }
    }

    #[test]
    fn api_key_options_use_consistent_help() {
        for path in [&["update"][..], &["get"][..], &["download"][..]] {
            assert!(
                render_help(path)
                    .contains("この実行で使う EDINET API キー(登録済みキーより優先)")
            );
        }
    }

    #[test]
    fn short_and_long_help_flags_have_identical_output() {
        for command in [
            &[][..],
            &["setup"][..],
            &["update"][..],
            &["search"][..],
            &["get"][..],
            &["download"][..],
            &["clear"][..],
            &["status"][..],
        ] {
            let mut short_args = vec!["edinet"];
            short_args.extend_from_slice(command);
            short_args.push("-h");

            let mut long_args = vec!["edinet"];
            long_args.extend_from_slice(command);
            long_args.push("--help");

            assert_eq!(
                help_output(&short_args),
                help_output(&long_args),
                "help output differs for `{}`",
                command.join(" ")
            );
        }
    }
}