@@ -638,6 +638,16 @@ static int has_subcommands(const struct option *options)
638638 return 0 ;
639639}
640640
641+ static void set_strdup_fn (struct parse_opt_ctx_t * ctx , const struct option * options ) {
642+ for (; options -> type != OPTION_END ; options ++ )
643+ ;
644+ if (options -> value && options -> strdup_fn ) {
645+ ctx -> unknown_opts = options -> value ;
646+ ctx -> strdup_fn = options -> strdup_fn ;
647+ return ;
648+ }
649+ }
650+
641651static void parse_options_start_1 (struct parse_opt_ctx_t * ctx ,
642652 int argc , const char * * argv , const char * prefix ,
643653 const struct option * options ,
@@ -655,6 +665,7 @@ static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
655665 ctx -> cpidx = ((flags & PARSE_OPT_KEEP_ARGV0 ) != 0 );
656666 ctx -> flags = flags ;
657667 ctx -> has_subcommands = has_subcommands (options );
668+ set_strdup_fn (ctx , options );
658669 if (!ctx -> has_subcommands && (flags & PARSE_OPT_SUBCOMMAND_OPTIONAL ))
659670 BUG ("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands" );
660671 if (ctx -> has_subcommands ) {
@@ -981,7 +992,11 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
981992 *
982993 * This is leaky, too bad.
983994 */
984- ctx -> argv [0 ] = xstrdup (ctx -> opt - 1 );
995+ if (ctx -> unknown_opts && ctx -> strdup_fn ) {
996+ ctx -> argv [0 ] = ctx -> strdup_fn (ctx -> unknown_opts , ctx -> opt - 1 );
997+ } else {
998+ ctx -> argv [0 ] = xstrdup (ctx -> opt - 1 );
999+ }
9851000 * (char * )ctx -> argv [0 ] = '-' ;
9861001 goto unknown ;
9871002 case PARSE_OPT_NON_OPTION :
0 commit comments