3333#include <xbps.h>
3434
3535#include "defs.h"
36+ #include "xbps.h"
37+ #include "xbps/json.h"
3638
3739static void __attribute__((noreturn ))
3840usage (bool fail )
@@ -46,6 +48,7 @@ usage(bool fail)
4648 " -F, --format <format> Format for list output\n"
4749 " -h, --help Show usage\n"
4850 " -i, --ignore-conf-repos Ignore repositories defined in xbps.d\n"
51+ " -J, --json Print output as json\n"
4952 " -M, --memory-sync Remote repository data is fetched and stored\n"
5053 " in memory, ignoring on-disk repodata archives\n"
5154 " -p, --property PROP[,...] Show properties for PKGNAME\n"
@@ -100,13 +103,14 @@ filter_repolock(xbps_object_t obj)
100103int
101104main (int argc , char * * argv )
102105{
103- const char * shortopts = "C:c:dF:f:hHiLlMmOo :p:Rr:s:S:VvX:x:" ;
106+ const char * shortopts = "C:c:dF:f:hHiJLlMmOo :p:Rr:s:S:VvX:x:" ;
104107 const struct option longopts [] = {
105108 { "config" , required_argument , NULL , 'C' },
106109 { "cachedir" , required_argument , NULL , 'c' },
107110 { "debug" , no_argument , NULL , 'd' },
108111 { "help" , no_argument , NULL , 'h' },
109112 { "ignore-conf-repos" , no_argument , NULL , 'i' },
113+ { "json" , no_argument , NULL , 'J' },
110114 { "list-repos" , no_argument , NULL , 'L' },
111115 { "list-pkgs" , no_argument , NULL , 'l' },
112116 { "list-hold-pkgs" , no_argument , NULL , 'H' },
@@ -137,6 +141,7 @@ main(int argc, char **argv)
137141 bool list_pkgs , list_repos , orphans , own , list_repolock ;
138142 bool list_manual , list_hold , show_prop , show_files , show_deps , show_rdeps ;
139143 bool show , pkg_search , regex , repo_mode , opmode , fulldeptree ;
144+ int json = 0 ;
140145
141146 rootdir = cachedir = confdir = props = pkg = catfile = format = NULL ;
142147 flags = rv = c = 0 ;
@@ -165,6 +170,9 @@ main(int argc, char **argv)
165170 case 'F' :
166171 format = optarg ;
167172 break ;
173+ case 'J' :
174+ json ++ ;
175+ break ;
168176 case 'H' :
169177 list_hold = opmode = true;
170178 break ;
@@ -286,20 +294,21 @@ main(int argc, char **argv)
286294 rv = repo_list (& xh );
287295
288296 } else if (list_hold ) {
289- rv = list_pkgdb (& xh , filter_hold , format ? format : "{pkgver}\n" ) < 0 ;
297+ rv = list_pkgdb (& xh , filter_hold , format ? format : "{pkgver}\n" , json ) < 0 ;
290298
291299 } else if (list_repolock ) {
292- rv = list_pkgdb (& xh , filter_repolock , format ? format : "{pkgver}\n" ) < 0 ;
300+ rv = list_pkgdb (& xh , filter_repolock , format ? format : "{pkgver}\n" , json ) < 0 ;
293301
294302 } else if (list_manual ) {
295- rv = list_pkgdb (& xh , filter_manual , format ? format : "{pkgver}\n" ) < 0 ;
303+ rv = list_pkgdb (& xh , filter_manual , format ? format : "{pkgver}\n" , json ) < 0 ;
296304
297305 } else if (list_pkgs ) {
298306 /* list available pkgs */
299- if (format )
300- rv = list_pkgdb (& xh , NULL , format );
301- else
307+ if (format || json > 0 ) {
308+ rv = list_pkgdb (& xh , NULL , format , json );
309+ } else {
302310 rv = list_pkgs_pkgdb (& xh );
311+ }
303312
304313 } else if (orphans ) {
305314 /* list pkg orphans */
0 commit comments