none
c# cannot covert commanlineoptions to system.type RRS feed

  • Question

  • ok created the following 

    public class CommandLineOptions
        {
            [CommandLine.Option('f', "file", Required = true, HelpText = "Input file or directory to be processed")]
            public string InputFile { get; set; }
    
            [CommandLine.Option('o', "out", HelpText = "The output file (or directory path of which to export to)")]
            public string OutputFile { get; set; }
    
            [CommandLine.Option('m', "mode", HelpText = "Tool export mode (export, bin)")]
            public string Mode { get; set; }
    
            [CommandLine.Option('w', "watcher", HelpText = "Start a watcher for new export/bin files")]
            public bool Watcher { get; set; }
            public string GetUsage()
            {
                // Instance a helper
                var help = new HelpText
                {
                    AdditionalNewLineAfterOption = false,
                    AddDashesToOption = true
                };
                // Add actual usage
                help.AddPreOptionsLine("Usage: " + Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location) + " -f <file to process> (Or drag and drop a file)");
                // Add all available options
                //help.AddOptions(this);
                // Return it
                return help;
            }

    and trying to call it down here and parse the flags and i get this error.

    static void Main(string[] args)
            {
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
    
    
    
                // Title
                Console.Title = "ExportX";
                // Header
                Console.WriteLine("-- ExportX--" + Environment.NewLine);
                // Handle arguments as follows:
                // If all args are files, and it each contains a bin or export, convert to opposite format, in same dir
                // Else, send to command parser,
                // If no args, print usage
                bool NeedsArgParse = (args.Length > 0) ? false : true;
                // Check for them
                foreach (string arg in args)
                {
                    // Check it
                    if (!arg.StartsWith("-") && !arg.Contains("\"") && File.Exists(arg))
                    {
                        // Not needed yet
                        NeedsArgParse = false;
                    }
                    else
                    {
                        // We need to parse args
                        NeedsArgParse = true;
                        // Stop
                        break;
                    }
                }
                
                // Check for multi-drag-drop
                if (!NeedsArgParse)
                {
                    // We detected multiple drag and drop, loop and convert
                    Parallel.ForEach<string>(args, (FilePath) =>
                    {
                        // Check for bin / export
                        string Compare = FilePath.ToLower();
                        // Check type
                        if (Compare.EndsWith(".xmodel_bin") || Compare.EndsWith(".xanim_bin"))
                        {
                            // Log it
                            Console.WriteLine(":  Converting \"" + Path.GetFileName(FilePath) + "\"");
                            // Convert it
                            using (XAssetFile file = new XAssetFile(FilePath))
                            {
                                // Write to export
                                file.WriteExport(Path.Combine(Path.GetDirectoryName(FilePath.ToLower()), Path.GetFileName(FilePath.ToLower()).Replace(".xanim_bin", ".xanim_export").Replace(".xmodel_bin", ".xmodel_export")));
                            }
                        }
                        else if (Compare.EndsWith(".xmodel_export") || Compare.EndsWith(".xanim_export"))
                        {
                            // Log it
                            Console.WriteLine(":  Converting \"" + Path.GetFileName(FilePath) + "\"");
                            // Convert it
                            using (XAssetFile file = new XAssetFile(FilePath))
                            {
                                // Write to bin
                                file.WriteBin(Path.Combine(Path.GetDirectoryName(FilePath.ToLower()), Path.GetFileName(FilePath.ToLower()).Replace(".xanim_export", ".xanim_bin").Replace(".xmodel_export", ".xmodel_bin")));
                            }
                        }
                    });
    
                    // Done
                    Console.Write(":  Finished converting...");
                    Console.ReadKey();
                }
                else
                {
                    // We must parse the flags because we had one that wasn't a file
                    CommandLineOptions options = new CommandLineOptions();
                    // Commands
                    if (Parser.Default.ParseArguments(args, options))
                             
                    {
                        // Check for *, if so, search current dir
                        if (options.InputFile == "*")
                        {
                            // Set to this
                            options.InputFile = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                        }
                        // Check for watcher
                        if (options.Watcher)
                        {
                            // Watcher only mode
                            if (Directory.Exists(options.InputFile))
                            {
                                // Setup filters
                                switch (options.Mode.ToLower())
                                {
                                    case "bin":
                                        WatcherManager.RunWatcher(options.InputFile, ".xmodel_export|.xanim_export", InFormat.Export);
                                        break;
                                    case "export":
                                        WatcherManager.RunWatcher(options.InputFile, ".xmodel_bin|.xanim_bin", InFormat.Bin);
                                        break;
                                }
                                
                            }
                            else
                            {
                                // Failed
    
                            }
                        }
                        else
                        {
                            // Check whether or not we got a file or directory
                            var info = File.GetAttributes(options.InputFile);
                            // Check
                            if (info.HasFlag(FileAttributes.Directory))
                            {
                                // Search directory for files based on the mode
                                if (Directory.Exists(options.InputFile))
                                {
                                    // Search for files based on mode in dir
                                    List<string> FilesToProcess = new List<string>();
                                    // Search for files opposite of the mode
                                    if (options.Mode.ToLower() == "bin")
                                    {
                                        // Get exports
                                        FilesToProcess.AddRange(Directory.GetFiles(options.InputFile, "*.xmodel_export", SearchOption.AllDirectories));
                                        FilesToProcess.AddRange(Directory.GetFiles(options.InputFile, "*.xanim_export", SearchOption.AllDirectories));
                                        // Log count
                                        Console.WriteLine(":  Converting " + FilesToProcess.Count + " files to bins");
                                    }
                                    else if (options.Mode.ToLower() == "export")
                                    {
                                        // Get bins
                                        FilesToProcess.AddRange(Directory.GetFiles(options.InputFile, "*.xmodel_bin", SearchOption.AllDirectories));
                                        FilesToProcess.AddRange(Directory.GetFiles(options.InputFile, "*.xanim_bin", SearchOption.AllDirectories));
                                        // Log count
                                        Console.WriteLine(":  Converting " + FilesToProcess.Count + " files to exports");
                                    }
                                    // Check output directory
                                    if (!string.IsNullOrEmpty(options.OutputFile) && !Directory.Exists(options.OutputFile))
                                    {
                                        // Make it
                                        Directory.CreateDirectory(options.OutputFile);
                                    }
                                    // Loop in parallel to convert them
                                    Parallel.ForEach<string>(FilesToProcess, (ToConv) =>
                                    {
                                        // Process it
                                        var ExportPath = string.Empty;
                                        // Check
                                        if (options.Mode.ToLower() == "export")
                                        {
                                            ExportPath = (string.IsNullOrEmpty(options.OutputFile)) ? ToConv.ToLower().Replace(".xanim_bin", ".xanim_export").Replace(".xmodel_bin", ".xmodel_export") : Path.Combine(options.OutputFile, Path.GetFileName(ToConv.ToLower().Replace(".xanim_bin", ".xanim_export").Replace(".xmodel_bin", ".xmodel_export")));
                                        }
                                        else
                                        {
                                            ExportPath = (string.IsNullOrEmpty(options.OutputFile)) ? ToConv.ToLower().Replace(".xanim_export", ".xanim_bin").Replace(".xmodel_export", ".xmodel_bin") : Path.Combine(options.OutputFile, Path.GetFileName(ToConv.ToLower().Replace(".xanim_export", ".xanim_bin").Replace(".xmodel_export", ".xmodel_bin")));
                                        }
                                        // Process and export
                                        using (XAssetFile Converter = new XAssetFile(ToConv))
                                        {
                                            // Save it to opposite type
                                            switch (Converter.Format)
                                            {
                                                case InFormat.Bin:
                                                    Converter.WriteExport(ExportPath);
                                                    break;
                                                case InFormat.Export:
                                                    Converter.WriteBin(ExportPath);
                                                    break;
                                            }
                                        }
                                    });
                                    // Finished
                                    Console.WriteLine(":  Finished converting...");
                                }
                            }
                            else
                            {
                                // Process single file
                                if (File.Exists(options.InputFile))
                                {
                                    // Process input
                                    using (XAssetFile Converter = new XAssetFile(options.InputFile))
                                    {
                                        // Check what we have
                                        if (options.Mode.ToLower() == "bin" && Converter.Format == InFormat.Export)
                                        {
                                            // Log
                                            Console.WriteLine(":  Converting \"" + Path.GetFileNameWithoutExtension(options.InputFile) + "\"");
                                            // Save to bin
                                            var ExportPath = (string.IsNullOrEmpty(options.OutputFile)) ? options.InputFile.ToLower().Replace(".xanim_export", ".xanim_bin").Replace(".xmodel_export", ".xmodel_bin") : options.OutputFile;
                                            // Save
                                            Converter.WriteBin(ExportPath);
                                            // Done
                                            Console.WriteLine(":  Finished converting...");
                                        }
                                        else if (options.Mode.ToLower() == "export" && Converter.Format == InFormat.Bin)
                                        {
                                            // Log
                                            Console.WriteLine(":  Converting \"" + Path.GetFileNameWithoutExtension(options.InputFile) + "\"");
                                            // Save to export
                                            var ExportPath = (string.IsNullOrEmpty(options.OutputFile)) ? options.InputFile.ToLower().Replace(".xanim_bin", ".xanim_export").Replace(".xmodel_bin", ".xmodel_export") : options.OutputFile;
                                            // Save
                                            Converter.WriteExport(ExportPath);
                                            // Done
                                            Console.WriteLine(":  Finished converting...");
                                        }
                                        else
                                        {
                                            // Format match
                                            Console.WriteLine(":  Input and output formats match.");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

    this part here 

                    // We must parse the flags because we had one that wasn't a file
                    CommandLineOptions options = new CommandLineOptions();
                    // Commands
                    if (Parser.Default.ParseArguments(args, options))

    tells me it cant convert options as its commandlineoption to system.type not sure how to resolve this.

    any help would be much appreciated thanks in advance elfenliedtopfan5

    Sunday, March 8, 2020 12:09 AM

All replies

  • https://docs.microsoft.com/en-us/dotnet/api/system.type?view=netframework-4.8

    CommandLineOptions is a class, a type, that you instanced into and object.  And you tried to use it on the next line of code that expects a System.Type.

    I don't know what you're trying to do,  but one type,  the class, can't be another type, becuase each one is unique that has its own properties and methods. It's like you're trying to tell a cow that it's going to be a pig.

    Sunday, March 8, 2020 3:57 AM
  • It appears that you are using a modern version of the Command Line parser library, but you are following the documentation in their website, which is outdated and no longer matches the version of the library. If you take the examples from their GitHub, these are up to date.

    The modern call is more ore less something like this:

    Parser.Default.ParseArguments<CommandLineOpcions>(args)

    But do check the GitHub repository for more details:

    https://github.com/commandlineparser/commandline

    Sunday, March 8, 2020 7:29 AM
    Moderator
  • this part here 
                    // We must parse the flags because we had one that wasn't a file
                    CommandLineOptions options = new CommandLineOptions();
                    // Commands
                    if (Parser.Default.ParseArguments(args, options))

    tells me it cant convert options as its commandlineoption to system.type not sure how to resolve this.

    any help would be much appreciated thanks in advance elfenliedtopfan5

    Somebody else already pointed out that you may be using the wrong documentation version versus code version, but on the off chance that your ParseArguments function is backward-compatible with itself then you might try:

    Parser.Default.ParseArguments(args, options.GetType())
    or else:
    Parser.Default.ParseArguments(args, typeof(CommandLineOptions))

    If it was backward-compatible then I would imagine it returned a new instance of CommandLineOptions of the specified type.  In that case you'd want to do something more like:

                    // We must parse the flags because we had one that wasn't a file
                    CommandLineOptions options = Parser.Default.ParseArguments(args, typeof(CommandLineOptions));
                    // Commands
                    if (options != null)

    This may not actually work, but it's worth a try.


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Sunday, March 8, 2020 4:44 PM