4
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.

ConsoleAppFramework(旧MicroBatchFramework)の入力文字列でパースしてくれる型について確認

Last updated at Posted at 2019-04-23

とのことで記事修正しました。

はじめに

ConsoleAppFrameworkでどのような入力をパースしてくれるか確認してみたので投稿します。
パースにはUtf8Jsonを使用していました。

be careful with JSON string double quotation.
JSON serializer is System.Text.Json. You can pass JsonSerializerOptions to SerivceProvider when you want to configure serializer behavior.


Githubから抜粋

JsonシリアライザーがSystem.Text.Jsonに変わったので注意です。

Stringとintは割愛。

環境

bool

        [Command("bool")]
        public void BoolParam([Option("x", "説明")]bool x)
        {
            Console.WriteLine(x.ToString());
            Environment.ExitCode = 0;
        }

としたとき

dotnet run -- bool -x false

で動作させたら

False

でOK

Enum

        public enum MyEnum
        {
            Enum1,
            Enum2
        }

        [Command("enum")]
        public void EnumParam([Option("x", "説明")]MyEnum x)
        {
            Console.WriteLine(Enum.GetName(typeof(MyEnum), x));
            Environment.ExitCode = 0;
        }

としたとき

dotnet run -- enum -x Enum1      

で動作させたら

Enum1

でOK

IEnumerable

        public void ListStringParam([Option("x", "説明")]List<string> x)
        {
            x.ForEach(item => Console.WriteLine(item));
            Environment.ExitCode = 0;
        }

        public void ListDoubleParam([Option("x", "説明")]List<double> x)
        {
            x.ForEach(item => Console.WriteLine(item));
            Environment.ExitCode = 0;
        }

        public void ListIntParam([Option("x", "説明")]List<int> x)
        {
            x.ForEach(item => Console.WriteLine(item));
            Environment.ExitCode = 0;
        }

としたとき


dotnet run -- listString -x "foo bar"    
dotnet run -- listDouble -x "0.5 1000000000000000000 2"
dotnet run -- listDouble -x "[0.5,1000000000000000000,2]"
dotnet run -- listInt -x "0 1 2"

で動作させたらOK(結果割愛)

json的に配列を示す"["と"]"がなくても自動補完してくれます。

独自クラス

        public class FooBar
        {
            public int foo { get; set; }

            public string bar { get; set; }
        }

        [Command("myClass")]
        public void MyClassParam([Option("x", "説明")]FooBar x)
        {
            Console.WriteLine(x.foo.ToString());
            Console.WriteLine(x.bar.ToString());
            Environment.ExitCode = 0;
        }

としたとき

dotnet run -- myClass -x "{`\`"foo`\`":0,`\`"bar`\`":`\`"str`\`"}"

で動作させたら

0
str

でOK
追加で

        public class FooBarBaz
        {
            [JsonPropertyName("foobar")]
            public FooBar FooBarProperty { get; set; }

            [JsonPropertyName("baz")]
            public string BazProperty { get; set; }
        }

         [Command("myClass2")]
       public void MyClassParam2([Option("x", "説明")]FooBarBaz x)
        {
            Console.WriteLine(x.FooBarProperty.foo);
            Console.WriteLine(x.FooBarProperty.bar);
            Console.WriteLine(x.BazProperty);
            Environment.ExitCode = 0;
        }

として

dotnet run -- myClass2 -x "{`\`"foobar`\`":{`\`"foo`\`":0,`\`"bar`\`":`\`"str`\`"},`\`"baz`\`":`\`"str2`\`"}"

で動作させたら

0
str
str2

で入れ子でも問題なし。

まとめ

なんでもいけそうです。困ったらSystem.Text.Jsonを参照すると良さげです。
今回powershellからdotnet runを実行したのですがダブルクォーテーション(")のエスケープ処理が面倒くさかったです。
C#的にはバックスラッシュ(\)でエスケープが必要でpowershell的にはバッククォート(`)でエスケープが必要です。
なので

"

を入力させるのに

`\`"

↑だけ必要です。

参考

4
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
4
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?