How to use Command Line Parser Library in case Error happend?

demo

I use Command Line Parser Library to parse command lines parameters.

I have declared class Options

internal class Options
{
    [Option('r', "read", Required = true,
        HelpText = "Input file to be processed.")]
    public string InputFile { get; set; }

    [Option('f', "date from", Required = false,
        HelpText = "Date from which get statistic.")]
    public string DateFrom { get; set; }

    [Option('t', "date to", Required = false,
        HelpText = "Date to which get statistic.")]
    public string DateTo { get; set; }

    [Option('v', "verbose", DefaultValue = true,
        HelpText = "Prints all messages to standard output.")]
    public bool Verbose { get; set; }

    [ParserState]
    public IParserState LastParserState { get; set; }

    [HelpOption]
    public string GetUsage()
    {
        return HelpText.AutoBuild(this,
            (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
    }
}

And this is how i try to use Parser :

private static void Main(string[] args)
    {
        Console.WriteLine("Hello and welcome to a test application!");

        string filePath = string.Empty;
        string fromDate = string.Empty, toDate = string.Empty;
        DateTime dateTo, dateFrom;

        Console.ReadLine();

        var options = new Options();
        if (CommandLine.Parser.Default.ParseArguments(args, options))
        {
            // Values are available here
            if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);

            if (string.IsNullOrWhiteSpace(options.InputFile))
            {
                filePath = ConfigurationManager.AppSettings["FilePath"];
                if (string.IsNullOrWhiteSpace(filePath))
                {
                    filePath = Directory.GetCurrentDirectory() + @"\Report.xlsx";
                }
            }
            else
            {
                filePath = options.InputFile;
            }
            fromDate = string.IsNullOrWhiteSpace(options.DateFrom)
                ? ConfigurationManager.AppSettings["DateFrom"]
                : options.DateFrom;
            toDate = string.IsNullOrWhiteSpace(options.DateTo) ? ConfigurationManager.AppSettings["DateTo"] : options.DateTo;
        }
        else
        {
            return;
        }

//other code }

But in case of some error application just stop working.

So i want to know how to repeat first step of inputting values in case of error.

while (!CommandLine.Parser.Default.ParseArguments(args, options)){...} - makes loop
Steven Scott

I use the Command Line Parser as such.

Declare a common wrapper for multiple projects

public class CommandLineOptions
{
    public const bool CASE_INSENSITIVE = false;
    public const bool CASE_SENSITIVE = true;
    public const bool MUTUALLY_EXCLUSIVE = true;
    public const bool MUTUALLY_NONEXCLUSIVE = false;
    public const bool UNKNOWN_OPTIONS_ERROR = false;
    public const bool UNKNOWN_OPTIONS_IGNORE = true;

    public CommandLineOptions();

    public string[] AboutText { get; set; }
    [ParserState]
    public IParserState LastParserState { get; set; }

    [HelpOption(HelpText = "Display this Help Screen")]
    public virtual string GetUsage();
    public bool ParseCommandLine(string[] Args);
    public bool ParseCommandLine(string[] Args, bool IgnoreUnknownOptions);
    public bool ParseCommandLine(string[] Args, bool IgnoreUnknownOptions, bool EnableMutuallyExclusive);
    public bool ParseCommandLine(string[] Args, bool IgnoreUnknownOptions, bool EnableMutuallyExclusive, bool UseCaseSensitive);
}

Create an Application Command Line class for this application

public class ApplicationCommandLine : CommandLineOptions
{
    [Option('d', "download", HelpText = "Download Items before running")]
    virtual public bool Download { get; set; }

    [Option('g', "generate", HelpText = "Generate Mode (Generate New Test Results)", MutuallyExclusiveSet = "Run-Mode")]
    virtual public bool GenerateMode { get; set; }

    [Option('r', "replay", HelpText = "Replay Mode (Run Test)", MutuallyExclusiveSet = "Run-Mode")]
    virtual public bool ReplayMode { get; set; }
}

Main Program:

ApplicationCommandLine AppCommandLine = new ApplicationCommandLine();

try
{
    // Parsers and sets the variables in AppCommandLine
    if (AppCommandLine.ParseCommandLine(args))
    {
        // Use the Download option from the command line.
        if (AppCommandLine.Download) { 
            DataFileDownload();
        }
        if (AppCommandLine.GenerateMode) { 
            GenerateProcessingData();
        }

        ...

    }
}

catch (Exception e)
{
    ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use Command Line Parser Library in case Error happend?

From Dev

Command Line Parser Library - Boolean Parameter

From Dev

Passing Guid to Command Line parser Library in C#

From Dev

Command Line Parser Library .NET being able to retrieve version number

From Dev

How to pipe data to rapper (command-line RDF parser by Redland)

From Dev

How can I use regex with sed (or equivalent unix command line tool) to fix title case in LaTeX headings?

From Dev

vi: how to use a line as a command?

From Dev

How to use a proxy on the command line?

From Dev

How to use this command line in Ubuntu

From Dev

how to use printf on the command line?

From Dev

How to use play framework config library command line parameters in non play application

From Dev

Haskell: Turtle: command line parser

From Dev

Error trying to use bluemix command line

From Dev

How to get output of system command in case of error?

From Dev

How to use knitr from command line with Rscript and command line argument?

From Dev

How to use Java JSch library to read remote file line by line?

From Dev

How to use "FLAGS" (command line switches) in TensorFlow?

From Dev

How to use paket from command line

From Dev

How to store command line arguments for future use?

From Dev

How to use ImageMagick command line on Windows?

From Dev

How to use environment modules in a ssh command line?

From Dev

How to use kapt from command line (with kotlinc)?

From Dev

How to use command line completion in Midnight Commander?

From Dev

How to use setvcpus from libvirt command line?

From Dev

How to use command line to change brightness and color?

From Dev

How to use command line to create a file with timestamp

From Dev

How to use execute mysqldiff utilities in command line?

From Dev

How to use command line to change volume?

From Dev

how to use passing arguments in command line

Related Related

  1. 1

    How to use Command Line Parser Library in case Error happend?

  2. 2

    Command Line Parser Library - Boolean Parameter

  3. 3

    Passing Guid to Command Line parser Library in C#

  4. 4

    Command Line Parser Library .NET being able to retrieve version number

  5. 5

    How to pipe data to rapper (command-line RDF parser by Redland)

  6. 6

    How can I use regex with sed (or equivalent unix command line tool) to fix title case in LaTeX headings?

  7. 7

    vi: how to use a line as a command?

  8. 8

    How to use a proxy on the command line?

  9. 9

    How to use this command line in Ubuntu

  10. 10

    how to use printf on the command line?

  11. 11

    How to use play framework config library command line parameters in non play application

  12. 12

    Haskell: Turtle: command line parser

  13. 13

    Error trying to use bluemix command line

  14. 14

    How to get output of system command in case of error?

  15. 15

    How to use knitr from command line with Rscript and command line argument?

  16. 16

    How to use Java JSch library to read remote file line by line?

  17. 17

    How to use "FLAGS" (command line switches) in TensorFlow?

  18. 18

    How to use paket from command line

  19. 19

    How to store command line arguments for future use?

  20. 20

    How to use ImageMagick command line on Windows?

  21. 21

    How to use environment modules in a ssh command line?

  22. 22

    How to use kapt from command line (with kotlinc)?

  23. 23

    How to use command line completion in Midnight Commander?

  24. 24

    How to use setvcpus from libvirt command line?

  25. 25

    How to use command line to change brightness and color?

  26. 26

    How to use command line to create a file with timestamp

  27. 27

    How to use execute mysqldiff utilities in command line?

  28. 28

    How to use command line to change volume?

  29. 29

    how to use passing arguments in command line

HotTag

Archive