gsscoder/commandline

Parser state

GrodanBollen opened this issue · 1 comments

Hi

After updating commandLineParser this error occurred:

Error CS0246 The type or namespace name 'IParserState' could not be found (are you missing a using directive or an assembly reference?)

and on more types/namespaces.

It also complains in the main method:

Error CS1503 Argument 2: cannot convert from 'xxx.Options' to 'System.Type'

My usings:
using CommandLine;
using CommandLine.Text;

I Also had to change my "DefaultValue" to "Default".

`static void Main(string[] args)
{
try
{

            var options = new Options();
            //options.ExportType = "full";

            if (Parser.Default.ParseArguments(args, options))
            {
                xxxx service = new xxxx();

                List<xxxx> users = null;
                // Values are available here
                if (options.Verbose) Console.WriteLine("Exporttyp: {0}", options.ExportType);
                if (options.ExportType.ToLower() == "full")
                {
                    //service.GetAllUsers();
                    //users = service.GetUsers();
                    users = service.GetAllUsers();
                }
                else if (options.ExportType.ToLower() == "diff")
                {
                    users = service.GetDiffUsers();
                }
                if (users != null && users.Count != 0)
                {
                    service.WriteExportFile(users, options.ExportFile, options.ExportType);
                }
                xxxx item = null;

                users.Add(item);
            }
        }`

`public class Options
{
[Option('t', "exporttyp", Required = true, HelpText = "Typ av export. Full eller Diff.")]
public string ExportType { get; set; }

    [Option('f', "path", Required = false,
        Default = "\\\\xxxx\\yyyy\\ggg\\gg\\ggg\\",
        HelpText = "Sökväg fil. Standard är ??????")]


    public string ExportFile { get; set; }

    [Option('v', "verbose", Default = false,
        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));
    }
}`

[ParserState], [HelpOption] and HelpText.AutoBuild also has issues.

Any ideas?

nemec commented

Hi @GrodanBollen we forked this repository to a new organization. Version 2.x is virtually a complete rewrite of the library by the original author and concepts like parser state and helpoption were removed.

The current documentation around parsing can be found here.

From a quick look at your code, this is mostly what you'll need to change:

  1. Remove all reference to parser state.
  2. Remove the GetUsage method.
  3. if(Parser.Default.ParseArguments(args, options)) { ... } should look like:
var result = Parser.Default.ParseArguments<Options>(args);
result.WithParsed(options => {
    // your code here
});

If you have further questions, please open up an issue in the new repository :)