The C# Simple Command Line Parameters Reader


In some cases, you just need to write a console application and want to be able to pass in different command line parameters as key/pairs. You can use the following C# Command Line Reader, which is simple enough, and easy to use (only 1 file, 1 class).

The full C# source code is available at github.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
    Author: https://helloacm.com
    Free to Use, donation is appreciated:  https://helloacm.com/out/paypal
*/
using System;
using System.Collections.Generic;
 
namespace CommandLineParametersReader
{
    public class SimpleCommandLineParametersReader
    {       
        private readonly char SepChar = '=';
        private string[] _args { get; }
        private Dictionary<string, string> _dict { get; }
 
        private bool CaseSensitive { get; }
 
        public CommandLineParametersReader(string[] args, bool isCaseSesnive = false)
        {
            if (args == null)
            {
                throw new Exception("args should not be null");
            }
            _args = args;
            CaseSensitive = isCaseSesnive;
            _dict = new Dictionary<string, string>();
            Process();            
        }
 
        // Process Arguments into KeyPairs
        private void Process()
        {
            foreach (var arg in _args)
            {
                var s = arg.Trim();
                var ss = s.Split(new []{ SepChar }, 2, StringSplitOptions.RemoveEmptyEntries);
                if (ss.Length <= 0) continue;
                var key = ss[0];
                if (!CaseSensitive)
                {
                    key = key.ToLower();
                }
                var val = ss[1];
                _dict[key] = val;
            }
        }
 
        // Return the Key with a default value
        public string Get(string key, string defaultvalue = "")
        {
            if (!CaseSensitive) { key = key.ToLower();}
            return _dict.ContainsKey(key) ? _dict[key] : defaultvalue;
        }
    }
}
/*
    Author: https://helloacm.com
    Free to Use, donation is appreciated:  https://helloacm.com/out/paypal
*/
using System;
using System.Collections.Generic;

namespace CommandLineParametersReader
{
    public class SimpleCommandLineParametersReader
    {       
        private readonly char SepChar = '=';
        private string[] _args { get; }
        private Dictionary<string, string> _dict { get; }

        private bool CaseSensitive { get; }

        public CommandLineParametersReader(string[] args, bool isCaseSesnive = false)
        {
            if (args == null)
            {
                throw new Exception("args should not be null");
            }
            _args = args;
            CaseSensitive = isCaseSesnive;
            _dict = new Dictionary<string, string>();
            Process();            
        }

        // Process Arguments into KeyPairs
        private void Process()
        {
            foreach (var arg in _args)
            {
                var s = arg.Trim();
                var ss = s.Split(new []{ SepChar }, 2, StringSplitOptions.RemoveEmptyEntries);
                if (ss.Length <= 0) continue;
                var key = ss[0];
                if (!CaseSensitive)
                {
                    key = key.ToLower();
                }
                var val = ss[1];
                _dict[key] = val;
            }
        }

        // Return the Key with a default value
        public string Get(string key, string defaultvalue = "")
        {
            if (!CaseSensitive) { key = key.ToLower();}
            return _dict.ContainsKey(key) ? _dict[key] : defaultvalue;
        }
    }
}

Example Usage:

1
2
3
4
var param = new SimpleCommandLineParametersReader(args);
param.CaseSensitive = True;
var input = param.Get("Input");  // suppose you have command line parameter "Input=InputFile"
var Output = param.Get("Output", "DefaultOutput");
var param = new SimpleCommandLineParametersReader(args);
param.CaseSensitive = True;
var input = param.Get("Input");  // suppose you have command line parameter "Input=InputFile"
var Output = param.Get("Output", "DefaultOutput");

The C++ version is also implemented, and can be found in this post.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
325 words
Last Post: How to Tell Browsers Re-update CSS/JS files when Files are Changed in WordPress?
Next Post: C++ Simple Command Line Parameters Reader

The Permanent URL is: The C# Simple Command Line Parameters Reader

Leave a Reply