Skip to content

Commit 84e5fe8

Browse files
committed
feat: add --format option for output formatting
- Add --format option with support for text, json, and json_pp formats - json_pp provides pretty-printed JSON output for human readability - Maintain backward compatibility with existing --json option - Resolves #251
1 parent a01f420 commit 84e5fe8

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
### Added
9+
10+
- Added `--format` option to specify output format: `text` (default), `json`, or `json_pp` (pretty-printed JSON)
11+
- Pretty-printed JSON output support via `--format json_pp` option
12+
13+
<!--
14+
### Changed
15+
16+
- A bullet item for the Changed category.
17+
18+
-->
19+
<!--
20+
### Fixed
21+
22+
- A bullet item for the Fixed category.
23+
24+
-->
25+
### Deprecated
26+
27+
- Deprecated `--json` option in favor of `--format json` (backward compatibility maintained)
28+
29+
<!--
30+
### Removed
31+
32+
- A bullet item for the Removed category.
33+
34+
-->
35+
<!--
36+
### Security
37+
38+
- A bullet item for the Security category.
39+
40+
-->
41+
<!--
42+
### Infrastructure
43+
44+
- A bullet item for the Infrastructure category.
45+
46+
-->

src/main.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ export async function main(): Promise<ValidationResult> {
3333
// Run the schema based validator
3434
const schemaResult = await validate(tree, options, config)
3535

36+
// Handle backward compatibility: if --json is used, override format
37+
const outputFormat = options.json ? 'json' : (options.format || 'text')
38+
3639
let output_string = ''
37-
if (options.json) {
38-
output_string = resultToJSONStr(schemaResult)
40+
if (outputFormat === 'json') {
41+
output_string = resultToJSONStr(schemaResult, false)
42+
} else if (outputFormat === 'json_pp') {
43+
output_string = resultToJSONStr(schemaResult, true)
3944
} else {
4045
output_string = consoleFormat(schemaResult, {
4146
verbose: options.verbose ? options.verbose : false,

src/setup/options.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export type ValidatorOptions = {
1919
datasetPath: string
2020
schema?: string
2121
config?: string
22-
json?: boolean
22+
json?: boolean // Deprecated, kept for backward compatibility
23+
format?: string
2324
verbose?: boolean
2425
ignoreNiftiHeaders?: boolean
2526
ignoreWarnings?: boolean
@@ -42,6 +43,8 @@ const modalityType = new EnumType<string>(
4243
Object.keys(schema.rules.modalities),
4344
)
4445

46+
const formatType = new EnumType<string>(['text', 'json', 'json_pp'])
47+
4548
/** Extendable Cliffy Command with built in BIDS validator options */
4649
export const validateCommand: Command<void, void, any, string[], void> = new Command()
4750
.name('bids-validator')
@@ -50,7 +53,13 @@ export const validateCommand: Command<void, void, any, string[], void> = new Com
5053
'This tool checks if a dataset in a given directory is compatible with the Brain Imaging Data Structure specification. To learn more about Brain Imaging Data Structure visit http://bids.neuroimaging.io',
5154
)
5255
.arguments('<dataset_directory>')
53-
.option('--json', 'Output machine readable JSON')
56+
.option('--json', '[Deprecated] Use --format json instead. Output machine readable JSON')
57+
.type('format', formatType)
58+
.option(
59+
'--format <format:format>',
60+
'Output format: text (default), json, or json_pp (pretty-printed JSON)',
61+
{ default: 'text' }
62+
)
5463
.option(
5564
'-s, --schema <URL-or-tag:string>',
5665
'Specify a schema version to use for validation',

src/utils/output.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ function helpUrl(code: string): string {
171171
return `https://neurostars.org/search?q=${code}`
172172
}
173173

174-
export function resultToJSONStr(result: ValidationResult): string {
174+
export function resultToJSONStr(result: ValidationResult, pretty: boolean = false): string {
175+
const indent = pretty ? 2 : 0
175176
return JSON.stringify(result, (key, value) => {
176177
if (value?.parent) {
177178
// Remove parent reference to avoid circular references
@@ -182,5 +183,5 @@ export function resultToJSONStr(result: ValidationResult): string {
182183
} else {
183184
return value
184185
}
185-
})
186+
}, indent)
186187
}

0 commit comments

Comments
 (0)