Candy.Updater.UpdateArgs.Parse C# (CSharp) Method

Parse() public static method

コマンドライン引数から UpdateArgs を生成します。
public static Parse ( string args ) : UpdateArgs
args string
return UpdateArgs
        public static UpdateArgs Parse(string[] args)
        {
            /*
             * ToolUpdater.exe /n "シャルロッテ" /p "D:\Tools\Charlotte" /u "http://192.168.47.74:8020/Charlotte/Update?v=1.12"
             * のようなコマンドライン引数をパース
             */

            var obj = new UpdateArgs
            {
                StepDelay = 400,
            };

            PropertyInfo property = null;
            foreach (var s in args.Where(x => x.Length > 0))
            {
                // スラッシュまたはハイフンで始まる場合をキーとみなす
                var c = s[0];
                if (c == '/' || c == '-')
                {
                    // キーに一致するプロパティを取得
                    _propertyDic.TryGetValue(s.Substring(1), out property);
                    // プロパティが Boolean の場合は指定された時点で true をセット
                    if (property.PropertyType == typeof(bool))
                    {
                        property.SetValue(obj, true);
                        property = null;
                    }
                    continue;
                }

                // 直前のコマンドライン引数がプロパティのキーだった場合は、今回の文字列を値とみなす
                if (property != null)
                {
                    property.SetValue(obj, s);
                }

                property = null;
            }

            return obj;
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                Converters =
                {
                    new VersionConverter()
                }
            };
            var commandLineArgs = UpdateArgs.Parse(args);

            Application.Run(new MainForm(commandLineArgs));
        }