Exemple #1
0
        private bool parse_remaining_arg(
            OptionGroup group,
            ref int idx,
            ref int argc, ref string[] argv,
            out bool parsed
            )
        {
            parsed = false;

            int j;

            for (j = 0; j < group.entries.Count; j++)
            {
                if (idx >= argc)
                {
                    return(true);
                }
                if (group.entries[j].long_name.Length > 0)
                {
                    continue;
                }

                switch (group.entries[j].arg)
                {
                case OptionArg.CALLBACK:
                case OptionArg.STRING_ARRAY:
                case OptionArg.FILENAME_ARRAY:
                    break;

                default:
                    return(false);
                }

                if (!parse_arg(group, group.entries[j], argv[idx], ""))
                {
                    return(false);
                }

                parsed = true;
                return(true);
            }
            return(true);
        }
Exemple #2
0
 public void add_group(OptionGroup group)
 {
     this.groups.Add(group);
 }
Exemple #3
0
        private bool parse_long_option(
            OptionGroup group,
            ref int idx,
            string arg,
            bool aliased,
            ref int argc,
            ref string[] argv,
            out bool parsed
            )
        {
            parsed = false;

            int j;

            for (j = 0; j < group.entries.Count; j++)
            {
                if (idx >= argc)
                {
                    return(true);
                }

                if (aliased && (group.entries[j].flags.HasFlag(OptionFlags.NOALIAS)))
                {
                    continue;
                }

                if (NO_ARG(group.entries[j]) && group.entries[j].long_name == arg)
                {
                    string option_name;
                    bool   retval;

                    option_name = "--" + group.entries[j].long_name;
                    retval      = parse_arg(group, group.entries[j], null, option_name);
                    parsed      = true;
                    return(retval);
                }
                else
                {
                    int len = group.entries[j].long_name.Length;
                    if (arg.StartsWith(group.entries[j].long_name) && (
                            arg.Length <= len || arg[len] == '='
                            ))
                    {
                        string value       = null;
                        string option_name = "--" + group.entries[j].long_name;

                        if (arg.Length > len && arg[len] == '=')
                        {
                            value = arg.Substring(len + 1);
                        }
                        else if (idx < argc - 1)
                        {
                            if (!OPTIONAL_ARG(group.entries[j]))
                            {
                                value = argv[idx + 1];
                                idx++;
                            }
                            else
                            {
                                if (argv[idx + 1][0] == '=')
                                {
                                    bool retval = parse_arg(group, group.entries[j], null, option_name);
                                    parsed = true;
                                    return(retval);
                                }
                                else
                                {
                                    value = argv[idx + 1];
                                    idx++;
                                }
                            }
                        }
                        else if (idx >= argc - 1 && OPTIONAL_ARG(group.entries[j]))
                        {
                            bool retval = parse_arg(group, group.entries[j], null, option_name);
                            parsed = true;
                            return(retval);
                        }
                        else
                        {
                            throw new Exception("Missing argument for " + option_name);
                        }

                        if (!parse_arg(group, group.entries[j], value, option_name))
                        {
                            return(false);
                        }

                        parsed = true;
                    }
                }
            }
            return(true);
        }
Exemple #4
0
        private bool parse_short_option(
            OptionGroup group,
            int idx,
            ref int new_idx,
            char arg,
            ref int argc,
            ref string[] argv,
            out bool parsed
            )
        {
            parsed = false;

            for (int j = 0; j < group.entries.Count; j++)
            {
                if (arg == group.entries[j].short_name)
                {
                    string value;
                    string option_name = group.entries[j].short_name.ToString();

                    if (NO_ARG(group.entries[j]))
                    {
                        value = null;
                    }
                    else
                    {
                        if (new_idx > idx)
                        {
                            throw new Exception("Error parsing option " + option_name);
                        }

                        if (idx < argc - 1)
                        {
                            if (!OPTIONAL_ARG(group.entries[j]))
                            {
                                value   = argv[idx + 1];
                                new_idx = idx + 1;
                            }
                            else
                            {
                                if (argv[idx + 1][0] == '-')
                                {
                                    value = null;
                                }
                                else
                                {
                                    value   = argv[idx + 1];
                                    new_idx = idx + 1;
                                }
                            }
                        }
                        else if (idx >= argc - 1 && OPTIONAL_ARG(group.entries[j]))
                        {
                            value = null;
                        }
                        else
                        {
                            throw new Exception("Missing argument for " + option_name);
                        }
                    }

                    if (!parse_arg(group, group.entries[j], value, option_name))
                    {
                        return(false);
                    }

                    parsed = true;
                }
            }
            return(true);
        }
Exemple #5
0
        private bool parse_arg(
            OptionGroup group,
            OptionEntry entry,
            string value,
            string option_name
            )
        {
            Debug.Assert(value != null || OPTIONAL_ARG(entry) || NO_ARG(entry));

            switch (entry.arg)
            {
            case OptionArg.NONE: {
                OptionEntry <bool> centry = entry as OptionEntry <bool>;
                centry.arg_data = !centry.flags.HasFlag(OptionFlags.REVERSE);
                break;
            }

            case OptionArg.STRING:
            case OptionArg.FILENAME: {
                OptionEntry <string> centry = entry as OptionEntry <string>;
                centry.arg_data = value;
                break;
            }

            case OptionArg.STRING_ARRAY:
            case OptionArg.FILENAME_ARRAY: {
                OptionEntry <IList <string> > centry = entry as OptionEntry <IList <string> >;
                if (centry.arg_data == null)
                {
                    centry.arg_data = new List <string>();
                }
                centry.arg_data.Add(value);
                break;
            }

            case OptionArg.INT: {
                OptionEntry <int> centry = entry as OptionEntry <int>;
                centry.arg_data = int.Parse(value);
                break;
            }

            case OptionArg.CALLBACK: {
                OptionEntry <OptionArgFunc> centry = entry as OptionEntry <OptionArgFunc>;
                string data = null;
                if (
                    centry.flags.HasFlag(OptionFlags.OPTIONAL_ARG) ||
                    centry.flags.HasFlag(OptionFlags.NO_ARG)
                    )
                {
                    data = null;
                }
                else if (centry.flags.HasFlag(OptionFlags.FILENAME))
                {
                    data = value;
                }
                if (centry.flags.HasFlag(OptionFlags.NO_ARG | OptionFlags.OPTIONAL_ARG) && data == null)
                {
                    return(false);
                }

                bool retval = centry.arg_data(option_name, data, group.user_data);
                if (!retval)
                {
                    throw new Exception("Error parsing option " + option_name);
                }
                return(retval);
            }

            case OptionArg.DOUBLE: {
                OptionEntry <double> centry = entry as OptionEntry <double>;
                centry.arg_data = double.Parse(value);
                break;
            }

            case OptionArg.INT64: {
                OptionEntry <Int64> centry = entry as OptionEntry <Int64>;
                centry.arg_data = Int64.Parse(value);
                break;
            }

            default:
                Debug.Assert(false);
                break;
            }
            return(true);
        }
Exemple #6
0
        private void print_help(bool main_help, OptionGroup group)
        {
            string help = get_help(main_help, group);

            Console.Write(help);
        }
Exemple #7
0
 public string get_help(bool main_help, OptionGroup group)
 {
     return(null);
 }