@@ -72,7 +72,7 @@ char *cmn_strndup(const char *str, size_t n)
7272
7373
7474
75- struct iio_context * autodetect_context (bool rtn , bool gen_code , const char * name )
75+ struct iio_context * autodetect_context (bool rtn , bool gen_code , const char * name , const char * scan )
7676{
7777 struct iio_scan_context * scan_ctx ;
7878 struct iio_context_info * * info ;
@@ -81,7 +81,7 @@ struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * na
8181 ssize_t ret ;
8282 FILE * out ;
8383
84- scan_ctx = iio_create_scan_context (NULL , 0 );
84+ scan_ctx = iio_create_scan_context (scan , 0 );
8585 if (!scan_ctx ) {
8686 fprintf (stderr , "Unable to create scan context\n" );
8787 return NULL ;
@@ -195,17 +195,19 @@ static const struct option common_options[] = {
195195 {"help" , no_argument , 0 , 'h' },
196196 {"xml" , required_argument , 0 , 'x' },
197197 {"uri" , required_argument , 0 , 'u' },
198- {"scan" , no_argument , 0 , 'S' },
199- {"auto" , no_argument , 0 , 'a' },
198+ {"scan" , optional_argument , 0 , 'S' },
199+ {"auto" , optional_argument , 0 , 'a' },
200200 {0 , 0 , 0 , 0 },
201201};
202202
203203static const char * common_options_descriptions [] = {
204204 "Show this help and quit." ,
205205 "Use the XML backend with the provided XML file." ,
206206 "Use the context at the provided URI." ,
207- "Scan for available backends." ,
208- "Scan for available contexts and if only one is available use it." ,
207+ "Scan for available backends."
208+ "\n\t\t\toptional arg of specific backend(s)" ,
209+ "Scan for available contexts and if only one is available use it."
210+ "\n\t\t\toptional arg of specific backend(s)" ,
209211};
210212
211213
@@ -222,6 +224,7 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
222224 opterr = 0 ;
223225 /* start over at first index */
224226 optind = 1 ;
227+
225228 while ((c = getopt_long (argc , argv , COMMON_OPTIONS , /* Flawfinder: ignore */
226229 common_options , & option_index )) != -1 ) {
227230 switch (c ) {
@@ -258,9 +261,21 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
258261 return NULL ;
259262 }
260263 detect_context = true;
264+ if (optarg ) {
265+ arg = optarg ;
266+ } else {
267+ if (argv [optind ] && argv [optind ][0 ] != '-' )
268+ arg = argv [optind ++ ];
269+ }
261270 break ;
262271 case 'S' :
263272 do_scan = true;
273+ if (optarg ) {
274+ arg = optarg ;
275+ } else {
276+ if (argv [optind ] && argv [optind ][0 ] != '-' )
277+ arg = argv [optind ++ ];
278+ }
264279 break ;
265280 case '?' :
266281 break ;
@@ -270,10 +285,10 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
270285 opterr = 1 ;
271286
272287 if (do_scan ) {
273- autodetect_context (false, false, name );
288+ autodetect_context (false, false, name , arg );
274289 exit (0 );
275290 } else if (detect_context )
276- ctx = autodetect_context (true, false, name );
291+ ctx = autodetect_context (true, false, name , arg );
277292 else if (!arg && backend != IIO_LOCAL )
278293 fprintf (stderr , "argument parsing error\n" );
279294 else if (backend == IIO_XML )
0 commit comments