1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio / WPF > コマンドライン引数 > 引数をパースして辞書に登録して使う

Last updated at Posted at 2017-04-30
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

コマンドライン引数をパースする。

IDEでのコマンドライン引数の設定方法は以下。
http://qiita.com/7of9/items/1e7999acaea13ecefe38

参考

コード

上記の2つ目のリンクは「--showText Hello」のような書式の扱いになっている。

一方で、こちらが使いたかった書式は「/lang=Urudu」のような書式。
Replace()やSplit()のメソッドチェーンを使って対応した。

adicをpublicにするのは良い実装ではない。
宣言と同時にインスタンス生成しているのも、さらに良くない。

MainWindow.xaml
<Window x:Class="_170430_t0820_runtimeParam.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170430_t0820_runtimeParam"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid Loaded="Grid_Loaded">
        <Button x:Name="button1" Content="button1" Click="button1_Click"/>
    </Grid>
</Window>
Mainwindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _170430_t0820_runtimeParam
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public Dictionary<string, string> adic = new Dictionary<string, string>();

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            string[] args = Environment.GetCommandLineArgs();

            for(int idx=0; idx < args.Length; idx++)
            {
                if (idx==0)  // command itself
                {
                    continue;
                }

                string[] wrk = args[idx].Replace("/", "")
                    .Replace("=", " ")
                    .Split(' ');

                adic.Add(wrk[0], wrk[1]); // e.g. lang, Urudu
            }

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            foreach(var elem in adic)
            {
                string msg = string.Format("key:{0}, value:{1}", elem.Key, elem.Value);
                MessageBox.Show(msg);
            }
        }
    }
}

work.png

work.png

work.png

v0.2 > /lang=設定によって、表示メッセージを変更

C# script

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _170430_t0820_runtimeParam
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public Dictionary<string, string> adic = new Dictionary<string, string>();

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            string[] args = Environment.GetCommandLineArgs();

            for(int idx=0; idx < args.Length; idx++)
            {
                if (idx==0)  // command itself
                {
                    continue;
                }

                string[] wrk = args[idx].Replace("/", "")
                    .Replace("=", " ")
                    .Split(' ');

                adic.Add(wrk[0], wrk[1]); // e.g. lang, Urudu
            }

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            foreach(var elem in adic)
            {
                //string msg = string.Format("key:{0}, value:{1}", elem.Key, elem.Value);
                //MessageBox.Show(msg);

                if (elem.Key=="lang")
                {
                    switch(elem.Value)
                    {
                        case "Urudu":
                            MessageBox.Show(" السلام علیکم");
                            break;
                        case "French":
                            MessageBox.Show("Bonjour!");
                            break;
                        default:
                            MessageBox.Show("Hello");
                            break;
                    }
                }
            }

        }
    }
}

結果 > Urudu

work.png

結果 > French

work.png

結果 > 引数なし

メッセージダイアログは表示されない。

lang=のパラメータがそもそもないため。
default:のところには来ない。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?