Skip to content

Commit ec28ec8

Browse files
authored
Merge pull request #4 from outscale-mgo/master
add -K option
2 parents 4f394e4 + f944a5d commit ec28ec8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

json-search.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int max_founds;
4545
#define PRINT_PARENT (1 << 4)
4646
#define CHECK_VALUE (1 << 5)
4747
#define LOCATION_INFO (1 << 6)
48+
#define PRINT_KEYS (1 << 7)
4849

4950
int program_flag;
5051

@@ -64,6 +65,7 @@ void usage(void)
6465
"\t-v: verbose mode\n"
6566
"\t-i: case insensitive\n"
6667
"\t-V: Check Value instead of keys\n"
68+
"\t-K: Print only objects keys\n"
6769
"\t-R: raw print\n"
6870
"\t-M NUM: limitate the number of returned objects\n"
6971
"\t-P: print parent instead of element\n"
@@ -108,6 +110,24 @@ int should_print_array(void)
108110
void print(const char *f, const char *k, struct json_object *v,
109111
int print_colun)
110112
{
113+
if (program_flag & PRINT_KEYS &&
114+
json_object_get_type(v) == json_type_object) {
115+
int loop = 0;
116+
if (!(program_flag & RAW_PRINT))
117+
putchar('[');
118+
json_object_object_foreach(v, k0, unused) {
119+
if (loop++) {
120+
putchar(',');
121+
}
122+
if (!(program_flag & RAW_PRINT))
123+
printf("\n ");
124+
printf("\"%s\"", k0);
125+
}
126+
if (!(program_flag & RAW_PRINT))
127+
printf("\n]");
128+
putchar('\n');
129+
return;
130+
}
111131
if (program_flag & RAW_PRINT) {
112132
if (json_object_get_type(v) == json_type_array ||
113133
json_object_get_type(v) == json_type_object) {
@@ -261,6 +281,10 @@ int main(int argc, char **argv)
261281
if (program_flag & VERBOSE)
262282
printf("print parent mode\n");
263283
program_flag |= PRINT_PARENT;
284+
} else if (*pc == 'K') {
285+
if (program_flag & VERBOSE)
286+
printf("print keys mode\n");
287+
program_flag |= PRINT_KEYS;
264288
} else if (*pc == 'r') {
265289
panic("-r not impemented reseved for recursive files search\n");
266290
} else if (*pc == 'o') {

0 commit comments

Comments
 (0)