diff options
Diffstat (limited to 'std/examples/catj.ts')
-rw-r--r-- | std/examples/catj.ts | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/std/examples/catj.ts b/std/examples/catj.ts index 4329d27b1..3ef14ce0b 100644 --- a/std/examples/catj.ts +++ b/std/examples/catj.ts @@ -81,28 +81,30 @@ function print(data: any): void { } } -const parsedArgs = parse(Deno.args); - -if (parsedArgs.h || parsedArgs.help || parsedArgs._.length === 0) { - console.log("Usage: catj [-h|--help] [file...]"); - console.log(); - console.log("Examples:"); - console.log(); - console.log("// print file:\n catj file.json"); - console.log(); - console.log("// print multiple files:\n catj file1.json file2.json"); - console.log(); - console.log("// print from stdin:\n cat file.json | catj -"); -} +if (import.meta.main) { + const parsedArgs = parse(Deno.args); + + if (parsedArgs.h || parsedArgs.help || parsedArgs._.length === 0) { + console.log("Usage: catj [-h|--help] [file...]"); + console.log(); + console.log("Examples:"); + console.log(); + console.log("// print file:\n catj file.json"); + console.log(); + console.log("// print multiple files:\n catj file1.json file2.json"); + console.log(); + console.log("// print from stdin:\n cat file.json | catj -"); + } -if (parsedArgs._[0] === "-") { - const contents = await Deno.readAll(Deno.stdin); - const json = JSON.parse(decoder.decode(contents)); - print(json); -} else { - for (const fileName of parsedArgs._) { - const fileContents = await Deno.readFile(fileName); - const json = JSON.parse(decoder.decode(fileContents)); + if (parsedArgs._[0] === "-") { + const contents = await Deno.readAll(Deno.stdin); + const json = JSON.parse(decoder.decode(contents)); print(json); + } else { + for (const fileName of parsedArgs._) { + const fileContents = await Deno.readFile(fileName.toString()); + const json = JSON.parse(decoder.decode(fileContents)); + print(json); + } } } |