Home - ArdaOzcan/SimpleArgParse GitHub Wiki

Welcome to the SimpleArgParse wiki!

SimpleArgParse Wiki

Welcome to SAP Wiki! Navigate through the sections to learn how to use SAP.

Quick Start

Positional Arguments

You can use positional arguments if an argument is required and has to be provided in a certain position.

If you pass in a name for the argument that does not start with the prefix "-", it will be parsed as a positional argument.

Example:

using System;
using ArdaOzcan.SimpleArgParse;

namespace Wiki
{
    class Program
    {
        static void Main(string[] args)
        {
            var parser = new ArgumentParser();
            parser.AddArgument("name");
            parser.AddArgument("surname");
            var parsedArgs = parser.ParseArgs(args);
            Console.WriteLine(parsedArgs);
        }
    }
}
> wiki.exe -h
usage: wiki [-h] name surname

Optional arguments:
  -h, --help            Show this message.

Positional arguments:
  name
  surname
> wiki.exe arda özcan
Namespace([name, arda], [surname, özcan])

Positional arguments are mandatory.

> wiki.exe
usage: wiki [-h] name
wiki : error: the following arguments are required: name

You can access positional variables just like a dictionary since Namespace derives from Dictionary<string, object>.

Example:

using System;
using ArdaOzcan.SimpleArgParse;

namespace Wiki
{
    class Program
    {
        static void Main(string[] args)
        {
            var parser = new ArgumentParser();
            parser.AddArgument("name");
            parser.AddArgument("surname");
            var parsedArgs = parser.ParseArgs(args);
            Console.WriteLine(parsedArgs["name"]);
        }
    }
}
> wiki.exe arda özcan
arda

Optional Arguments

If you start an argument name with "-" prefix, it will be an optional argument. Only Store action will be referenced here in the Quick Start section. Check the ArgumentAction section for more detailed information.

Example:

using System;
using ArdaOzcan.SimpleArgParse;

namespace Wiki
{
    class Program
    {
        static void Main(string[] args)
        {
            var parser = new ArgumentParser();
            parser.AddArgument("-n", "--name");
            var parsedArgs = parser.ParseArgs(args);
            Console.WriteLine(parsedArgs);
        }
    }
}
> wiki.exe -n arda
Namespace([name, arda])

As the name implies these arguments are optional, so optional arguments' values are null by default.

> wiki.exe
Namespace([name, ])