@@ -19,12 +19,13 @@ process_args <- function(args, app) {
1919 for (i in seq_along(app_opts )) {
2020 if (identical(short , app_opts [[i ]][[" short" ]])) {
2121 if (identical(app_opts [[i ]][[" arg_type" ]], " switch" )) {
22- if (has_positive_alias (app_opts [[i ]])) {
22+ if (show_positive_alias (app_opts [[i ]])) {
2323 return (paste0(" --" , names(app_opts )[[i ]]))
2424 }
25- if (has_negative_alias (app_opts [[i ]])) {
25+ if (show_negative_alias (app_opts [[i ]])) {
2626 return (paste0(" --no-" , names(app_opts )[[i ]]))
2727 }
28+ return (paste0(" --" , names(app_opts )[[i ]]))
2829 } else {
2930 return (paste0(" --" , names(app_opts )[[i ]]))
3031 }
@@ -86,6 +87,7 @@ process_args <- function(args, app) {
8687
8788 # resolve these values in this block
8889 name <- val <- spec <- NULL
90+ positive_switch <- FALSE
8991
9092 # --name=val
9193 equals_idx <- regexpr(" =" , a )
@@ -101,13 +103,15 @@ process_args <- function(args, app) {
101103 ) {
102104 stop_unrecognized_arg(a )
103105 }
106+ if (! is.null(spec ) && identical(spec $ arg_type , " switch" )) {
107+ positive_switch <- TRUE
108+ }
104109 if (is.null(spec ) && startsWith(a , " --no-" )) {
105110 alt_name <- str_drop_prefix(name , " no_" )
106111 alt_spec <- app_opts [[alt_name ]]
107112 if (
108113 ! is.null(alt_spec ) &&
109- identical(alt_spec $ arg_type , " switch" ) &&
110- ! has_negative_alias(alt_spec )
114+ identical(alt_spec $ arg_type , " switch" )
111115 ) {
112116 stop_unrecognized_arg(a )
113117 }
@@ -125,6 +129,9 @@ process_args <- function(args, app) {
125129 ) {
126130 stop_unrecognized_arg(a )
127131 }
132+ if (! is.null(spec ) && identical(spec $ arg_type , " switch" )) {
133+ positive_switch <- TRUE
134+ }
128135
129136 # if flag not known, maybe this is a switch flag
130137 if (is.null(spec ) && startsWith(a , " --no-" )) {
@@ -154,10 +161,28 @@ process_args <- function(args, app) {
154161
155162 if (is.null(val )) {
156163 if (identical(spec $ arg_type , " switch" )) {
157- val <- " true"
164+ if (positive_switch ) {
165+ next_arg <- readLines(args , 1L )
166+ if (length(next_arg )) {
167+ if (is_bool_cli_value(next_arg )) {
168+ val <- next_arg
169+ } else {
170+ pushBack(next_arg , args )
171+ }
172+ }
173+ }
174+ if (is.null(val )) {
175+ val <- " true"
176+ }
158177 } else {
159178 # arg_type == "option"
160179 val <- readLines(args , 1L )
180+ if (! length(val )) {
181+ stop(
182+ sprintf(" Missing value for --%s." , to_kebab_case(name )),
183+ call. = FALSE
184+ )
185+ }
161186 }
162187 }
163188
@@ -328,5 +353,11 @@ normalize_bool_cli_value <- function(val) {
328353 )
329354}
330355
356+ is_bool_cli_value <- function (val ) {
357+ is.character(val ) &&
358+ length(val ) == 1L &&
359+ normalize_bool_cli_value(val ) %in% c(" true" , " false" )
360+ }
361+
331362to_snake_case <- function (x ) gsub(" -" , " _" , x , fixed = TRUE )
332363to_kebab_case <- function (x ) gsub(" _" , " -" , x , fixed = TRUE )
0 commit comments