Skip to content

Commit 905ded7

Browse files
committed
basic cli fleshed out
1 parent 21e1169 commit 905ded7

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ If you just want the CLI, and don't use node, you can also find standalone build
2424

2525
### CLI
2626

27-
I am still working on this, but will offer a CLI for all the different things you can do with this lib.
27+
Install it in your path with `npm i -g rawproto` or use it 1-off with `npx rawproto`. Get help with `rawproto --help`
28+
2829

2930
### code
3031

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"module": "./dist/rawproto.module.js",
1313
"unpkg": "./dist/rawproto.umd.js",
1414
"bin": {
15-
"rawproto": "rawproto_cli.cjs"
15+
"rawproto": "rawproto_cli.js"
1616
},
1717
"dependencies": {
1818
"flat": "^6.0.1",

rawproto_cli.cjs renamed to rawproto_cli.js

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
#!/usr/bin/env node
22

3-
const yargs = require('yargs/yargs')
4-
const { hideBin } = require('yargs/helpers')
5-
const RawProto = require('./dist/rawproto.cjs')
3+
import yargs from 'yargs/yargs'
4+
import { hideBin } from 'yargs/helpers'
5+
import RawProto from './dist/rawproto.modern.js'
6+
import { readFile } from 'fs/promises'
67

78
async function getStdin () {
89
const chunks = []
910
for await (const chunk of process.stdin) chunks.push(chunk)
1011
return Buffer.concat(chunks)
1112
}
1213

13-
async function actionJson ({ types }) {
14-
const p = new RawProto(await getStdin(), types)
15-
console.log(JSON.stringify(p.toJS(), null, 2))
14+
async function actionJson ({ types, prefix }) {
15+
if (types) {
16+
types = JSON.parse(await readFile(types))
17+
} else {
18+
types = {}
19+
}
20+
const j = new RawProto(await getStdin()).toJS(types, prefix)
21+
console.log(JSON.stringify(j, null, 2))
1622
}
1723

18-
async function actionProto ({ types }) {
19-
const p = new RawProto(await getStdin(), types)
20-
console.log(p.toProto())
24+
async function actionProto ({ types, prefix }) {
25+
if (types) {
26+
types = JSON.parse(await readFile(types))
27+
} else {
28+
types = {}
29+
}
30+
const p = new RawProto(await getStdin(), types).toProto(types, prefix)
31+
console.log(p)
2132
}
2233

23-
async function actionQuery ({ query, types }) {
24-
const p = new RawProto(await getStdin(), types)
34+
async function actionQuery ({ query }) {
35+
const p = new RawProto(await getStdin())
2536
console.log(JSON.stringify(p.query(query), null, 2))
2637
}
2738

@@ -36,8 +47,15 @@ yargs(hideBin(process.argv))
3647
.positional('types', {
3748
describe: 'JSON file that selects types for fields'
3849
})
50+
.option('prefix', {
51+
alias: 'p',
52+
type: 'string',
53+
description: 'Path-prefix to add to fields on output',
54+
default: 'f'
55+
})
3956
.example('cat myfile.pb | $0 json', 'Get the JSON from myfile.pb, guessing types')
4057
.example('$0 json < myfile.pb', 'Get the JSON from myfile.pb, guessing types')
58+
.example('$0 json -p "" < myfile.pb', 'Get the JSON from myfile.pb, guessing types, oputput no field-prefixes')
4159
.example('$0 json choices.json < myfile.pb', 'Get the JSON from myfile.pb, using choices.json for types')
4260
}, (argv) => {
4361
actionJson(argv)
@@ -47,33 +65,35 @@ yargs(hideBin(process.argv))
4765
.positional('types', {
4866
describe: 'JSON file that selects types for fields'
4967
})
68+
.option('prefix', {
69+
alias: 'p',
70+
type: 'string',
71+
description: 'Path-prefix to add to fields on output',
72+
default: 'f'
73+
})
5074
.example('cat myfile.pb | $0 proto', 'Get the proto from myfile.pb, guessing types')
5175
.example('$0 proto < myfile.pb', 'Get the proto from myfile.pb, guessing types')
5276
.example('$0 proto choices.json < myfile.pb', 'Get the proto from myfile.pb, using choices.json for types')
5377
}, (argv) => {
5478
actionProto(argv)
5579
})
56-
.command('query <query> [types]', 'Output JSON (on stdout) for a specific query, for binary protobuf (on stdin)', (yargs) => {
80+
.command('query <query>', 'Output JSON (on stdout) for a specific query, for binary protobuf (on stdin)', (yargs) => {
5781
return yargs
5882
.positional('query', {
5983
describe: 'Your protobuf query string'
6084
})
61-
.positional('types', {
62-
describe: 'JSON file that selects types for fields'
63-
})
6485
.example('cat myfile.pb | $0 query 1.2.4.10.5:string', 'Get the query from myfile.pb')
6586
.example('$0 query 1.2.4.10.5:string < myfile.pb', 'Get the query from myfile.pb')
66-
.example('$0 query 1.2.4.10.5 choices.json < myfile.pb', 'Get the query from myfile.pb, using choices.json for types')
6787
}, (argv) => {
6888
actionQuery(argv)
6989
})
70-
.command('$0 types', 'Output JSON types guess (on stdout) for binary protobuf (on stdin)', (yargs) => {
71-
return yargs
72-
.example('cat myfile.pb | $0 types', 'Get the types from myfile.pb')
73-
.example('$0 types < myfile.pb', 'Get the types from myfile.pb')
74-
}, (argv) => {
75-
actionTypes(argv)
76-
})
90+
// .command('$0 types', 'Output JSON types guess (on stdout) for binary protobuf (on stdin)', (yargs) => {
91+
// return yargs
92+
// .example('cat myfile.pb | $0 types', 'Get the types from myfile.pb')
93+
// .example('$0 types < myfile.pb', 'Get the types from myfile.pb')
94+
// }, (argv) => {
95+
// actionTypes(argv)
96+
// })
7797
.example('$0 json --help', 'Get more detailed help on json')
7898
.example('$0 proto --help', 'Get more detailed help on proto')
7999
.example('$0 query --help', 'Get more detailed help on query')

test/hearthstone.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"id": "1.2.4.1:string",
3+
"title": "1.2.4.5:string",
4+
"company": "1.2.4.6:string",
5+
"description": "1.2.4.7:string"
6+
}

0 commit comments

Comments
 (0)