Skip to content

Commit c4f32c0

Browse files
committed
feat: Improve CLI usability, help, and documentation
This commit introduces a comprehensive set of improvements to enhance the tool's usability and documentation clarity. 1. **Correct Standard Input Handling:** The tool now correctly reads from standard input when no input file is specified or when '-' is used as the file name. This fixes a bug and allows for more flexible usage in command-line pipelines. 2. **Clearer Help Message:** The help message (`-h` flag) has been significantly improved. It now explicitly marks required flags (`-o`, `-t`) and provides a clearer, more detailed explanation of the tool's functionality, input methods, and a practical usage example. 3. **Synchronized Documentation:** The `README.md` and `README.ja.md` files have been updated to align with the actual behavior of the CLI. The "Usage" section now clearly explains the required flags and the optional nature of the input file argument, ensuring consistency between the code, help text, and documentation.
1 parent 36add5e commit c4f32c0

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

README.ja.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ make build
3535

3636
## 使い方
3737

38-
このツールは入力JSONファイルと以下のフラグを必要とします
38+
このツールは、必須のフラグ`-o``-t`に加えて、JSONデータを入力ファイルまたは標準入力経由で受け取ります
3939

4040
- `-o <パス>`: **必須。** 出力先のSQLiteデータベースファイルを指定します。
4141
- `-t <テーブル名>`: **必須。** 作成または更新するテーブル名を指定します。
4242
- `--version`: ツールのバージョン情報を表示します。
4343

44+
任意の引数 `[入力ファイル]` を指定すると、そのファイルからJSONを読み込みます。`[入力ファイル]` が省略されるか、`-` に設定された場合、ツールは標準入力から読み込みます。
45+
4446
### 使用例
4547

4648
**1. JSONファイルを新しいデータベースに変換する:**
@@ -50,9 +52,9 @@ json-to-sqlite -o users.db -t users users.json
5052

5153
**2. 他のコマンド(例: `curl`)からJSONデータをパイプで渡す:**
5254
```bash
53-
curl "https://api.example.com/data" | json-to-sqlite -o api_data.db -t records -
55+
curl "https://api.example.com/data" | json-to-sqlite -o api_data.db -t records
5456
```
55-
*注: 標準入力からパイプで渡す場合、`input_json_file`引数として`-`を使用してください*
57+
*注: 上記のコマンドは `[入力ファイル]` 引数を省略しているため機能します。引数として `-` を使用しても (`... | json-to-sqlite -o api_data.db -t records -`) 同じ結果になります*
5658

5759
**3. 新しいカラムを持つ可能性のあるデータを既存のデータベースに追加する:**
5860
```bash

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ make build
3535

3636
## Usage
3737

38-
The tool requires an input JSON file and the following flags:
38+
The tool has two required flags, `-o` and `-t`. JSON data can be provided either as an input file argument or via standard input.
3939

4040
- `-o <path>`: **Required.** Specifies the path for the output SQLite database file.
4141
- `-t <name>`: **Required.** Specifies the name of the table to create or update.
4242
- `--version`: Prints the current version of the tool.
4343

44+
An optional `[input_file]` argument can be provided to read from a file. If `[input_file]` is omitted or set to `-`, the tool will read from standard input.
45+
4446
### Examples
4547

4648
**1. Convert a JSON file into a new database:**
@@ -50,9 +52,9 @@ json-to-sqlite -o users.db -t users users.json
5052

5153
**2. Pipe JSON data from another command (e.g., `curl`):**
5254
```bash
53-
curl "https://api.example.com/data" | json-to-sqlite -o api_data.db -t records -
55+
curl "https://api.example.com/data" | json-to-sqlite -o api_data.db -t records
5456
```
55-
*Note: When piping from stdin, use `-` as the input_json_file argument.*
57+
*Note: The above command works because the `[input_file]` argument is omitted. Using `-` as the argument (`... | json-to-sqlite -o api_data.db -t records -`) achieves the same result.*
5658

5759
**3. Add new data with potentially new columns to an existing database:**
5860
```bash

main.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ func main() {
2828
tableName := flag.String("t", "", "Table name to insert data into (required)")
2929
versionFlag := flag.Bool("version", false, "Print version information")
3030

31+
flag.Usage = func() {
32+
fmt.Fprintln(os.Stderr, "Usage: json-to-sqlite -o <database.db> -t <table_name> [input_file]")
33+
fmt.Fprintln(os.Stderr, "")
34+
fmt.Fprintln(os.Stderr, "A tool to convert JSON data into an SQLite database.")
35+
fmt.Fprintln(os.Stderr, "")
36+
fmt.Fprintln(os.Stderr, "Reads JSON from a file or from standard input and creates a table with a schema")
37+
fmt.Fprintln(os.Stderr, "inferred from the JSON objects.")
38+
fmt.Fprintln(os.Stderr, "")
39+
fmt.Fprintln(os.Stderr, "If [input_file] is omitted or is '-', data is read from standard input.")
40+
fmt.Fprintln(os.Stderr, "")
41+
fmt.Fprintln(os.Stderr, "The JSON input must be an array of objects or a single JSON object.")
42+
fmt.Fprintln(os.Stderr, "")
43+
fmt.Fprintln(os.Stderr, "Options:")
44+
flag.PrintDefaults()
45+
fmt.Fprintln(os.Stderr, "")
46+
fmt.Fprintln(os.Stderr, "Example:")
47+
fmt.Fprintln(os.Stderr, " cat data.json | json-to-sqlite -o my.db -t my_table")
48+
}
49+
3150
flag.Parse()
3251

3352
if *versionFlag {
@@ -36,24 +55,18 @@ func main() {
3655
}
3756

3857
if *outputDB == "" {
39-
fmt.Println("Error: Output database file (-o) is required.")
40-
flag.PrintDefaults()
58+
fmt.Fprintln(os.Stderr, "Error: Output database file (-o) is required.\n")
59+
flag.Usage()
4160
os.Exit(1)
4261
}
4362

4463
if *tableName == "" {
45-
fmt.Println("Error: Table name (-t) is required.")
46-
flag.PrintDefaults()
64+
fmt.Fprintln(os.Stderr, "Error: Table name (-t) is required.\n")
65+
flag.Usage()
4766
os.Exit(1)
4867
}
4968

5069
// --- Input Handling ---
51-
if len(flag.Args()) == 0 {
52-
fmt.Println("Usage: json-to-sqlite [options] <input_json_file>")
53-
flag.PrintDefaults()
54-
os.Exit(1)
55-
}
56-
5770
reader, err := getInputReader(flag.Args())
5871
if err != nil {
5972
log.Fatal(err)
@@ -89,9 +102,13 @@ func main() {
89102
fmt.Printf("Successfully processed %d objects into table '%s' in database '%s'.\n", len(data), *tableName, *outputDB)
90103
}
91104

105+
92106
func getInputReader(args []string) (io.Reader, error) {
93107
if len(args) > 0 {
94108
filePath := args[0]
109+
if filePath == "-" {
110+
return os.Stdin, nil
111+
}
95112
file, err := os.Open(filePath)
96113
if err != nil {
97114
return nil, fmt.Errorf("error opening file %s: %w", filePath, err)
@@ -292,4 +309,4 @@ func insertData(db *sql.DB, tableName string, columns []string, data []map[strin
292309
}
293310

294311
return tx.Commit()
295-
}
312+
}

0 commit comments

Comments
 (0)