From ecfabf4db860ffd7466fd3c75b22e493638b27f1 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 15 Jul 2023 11:35:13 -0700 Subject: [PATCH] argparse: Use a named constant for RETURN_IN_ORDER returns The RETURN_IN_ORDER argparse mode (enabled via leading '-') causes non-options (i.e. positionals) to be returned intermixed with options in the original order, instead of being permuted to the end. Such positionals are identified via the option sentinel of char code 1. Use a real named constant for this return, rather than weird stuff like '\u{1}' --- fish-rust/src/builtins/argparse.rs | 4 ++-- fish-rust/src/wgetopt.rs | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/fish-rust/src/builtins/argparse.rs b/fish-rust/src/builtins/argparse.rs index d18dac750..3fd179b3e 100644 --- a/fish-rust/src/builtins/argparse.rs +++ b/fish-rust/src/builtins/argparse.rs @@ -12,7 +12,7 @@ use crate::ffi::Repin; use crate::wchar::{wstr, WString, L}; use crate::wchar_ext::WExt; use crate::wcstringutil::split_string; -use crate::wgetopt::{wgetopter_t, wopt, woption, woption_argument_t}; +use crate::wgetopt::{wgetopter_t, wopt, woption, woption_argument_t, NONOPTION_CHAR_CODE}; use crate::wutil::{fish_iswalnum, fish_wcstol, wgettext_fmt}; use libc::c_int; @@ -832,7 +832,7 @@ fn argparse_parse_flags<'args>( STATUS_CMD_OK } } - '\u{1}' => { + NONOPTION_CHAR_CODE => { // A non-option argument. // We use `-` as the first option-string-char to disable GNU getopt's reordering, // otherwise we'd get ignored options first and normal arguments later. diff --git a/fish-rust/src/wgetopt.rs b/fish-rust/src/wgetopt.rs index bc4224c98..8caddfe73 100644 --- a/fish-rust/src/wgetopt.rs +++ b/fish-rust/src/wgetopt.rs @@ -54,6 +54,9 @@ enum Ordering { RETURN_IN_ORDER, } +/// The special character code, enabled via RETURN_IN_ORDER, indicating a non-option argument. +pub const NONOPTION_CHAR_CODE: char = '\x01'; + impl Default for Ordering { fn default() -> Self { Ordering::PERMUTE @@ -323,7 +326,7 @@ impl<'opts, 'args, 'argarray> wgetopter_t<'opts, 'args, 'argarray> { } self.woptarg = Some(self.argv[self.woptind]); self.woptind += 1; - return Some(char::from(1)); + return Some(NONOPTION_CHAR_CODE); } // We have found another option-ARGV-element. Skip the initial punctuation.