Beispiel #1
0
        /// <summary>
        /// Parses a querystring as command-line arguments. Useful for Click-Once apps that are started from a URL.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryString"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static CmdLineParseResults <T> ParseArgs <T>(string queryString, CmdLineOptions options = null) where T : new()
        {
            options = options ?? CmdLineOptions.GetOptions <T>();
            var args = QueryStringToArgs(queryString, options);

            return(ParseArgs <T>(args, options));
        }
Beispiel #2
0
        /// <summary>
        /// Parses the default command-line arguments, retrieved from Environment.GetCommandLineArgs() (ignores the first argument which is the path). Also supports Click-Once deployed URL arguments.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="options"></param>
        /// <returns></returns>
        public static CmdLineParseResults <T> ParseArgs <T>(CmdLineOptions options = null) where T : new()
        {
            var args = Environment.GetCommandLineArgs();

            args = args.Skip(1).ToArray();             // MSDN: The first element in the array contains the file name of the executing program. If the file name is not available, the first element is equal to String.Empty. The remaining elements contain any additional tokens entered on the command line.
            return(ParseArgs <T>(args, options));
        }
Beispiel #3
0
        /// <summary>
        /// Converts a query string into an argument list that the parser can understand.
        /// </summary>
        /// <param name="queryString"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        internal static string[] QueryStringToArgs(string queryString, CmdLineOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (queryString.IsEmpty())
            {
                return new string[] { }
            }
            ;

            var args = new List <string>();
            var qs   = queryString;

            // If there is a ? in the querystring, remove it and everything to the left.
            var idx = qs.IndexOf('?');

            if (idx >= 0)
            {
                qs = qs.Substring(idx + 1);
            }

            // If there is a URL fragment, remove it.
            idx = qs.LastIndexOf('#');
            if (idx >= 0)
            {
                qs = qs.Substring(0, idx);
            }

            var pairs = qs.Split('&');

            foreach (var vp in pairs)
            {
                string name;
                string value;
                idx = vp.IndexOf('=');
                if (idx < 0)                 // Flag option.
                {
                    name  = vp.Trim();
                    value = null;                     // Just add the flag.
                }
                else
                {
                    name  = vp.Substring(0, idx).Trim();
                    value = vp.Substring(idx + 1);
                    value = Uri.UnescapeDataString(value);
                    value = value.Replace('+', ' ');                     // Uri.UnescapeDataString does not unescape +.
                }

                if (name.HasValue())
                {
                    args.Add(options.ArgumentPrefix + name);
                    if (value != null)
                    {
                        args.Add(value);
                    }
                }
            }

            return(args.ToArray());
        }
Beispiel #4
0
        /// <summary>
        /// Parses the supplied command-line arguments.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="args"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static CmdLineParseResults <T> ParseArgs <T>(IEnumerable <string> args, CmdLineOptions options = null) where T : new()
        {
            var parser = new CmdLineParser <T>(options);

            return(parser.Parse(args));
        }
Beispiel #5
0
 /// <summary>
 /// Creates an instance of CmdLineOptionsAttribute.
 /// </summary>
 public CmdLineOptionsAttribute()
 {
     // Start with the default options.
     CmdLineOptions = new CmdLineOptions();
 }