hackinit96
hackinit96

Reputation: 13

Creating a command line in C# that includes file path

I am trying to create a project that accepts a configuration file and 2 comparison files using a command line arguments with the paths to these files included. Would I construct this the same way you would pass any command line argument? Sorry I am new to this so I am not sure if there is an exception when trying to pass files.

Can I get an example of how this would be done? Here is a picture of the directions of what exactly I have been asked.

Accept the following command line arguments:

  1. Configuration file (with path) (described below)
  2. Comparison File 1 (with path)
  3. Comparison File 2 (with path)

Upvotes: 1

Views: 748

Answers (2)

Filip Cordas
Filip Cordas

Reputation: 2561

Because I really like this nuget(No association just a fan). Here is an example of it using CommandLineUtils First add an new project with dotnet new consol TestConsolUtils then add the nuget dotnet add package McMaster.Extensions.CommandLineUtils then copy this code to the program class.

using McMaster.Extensions.CommandLineUtils;
using System;

namespace ConsolUtilsTest
{
    class Program
    {
        public static int Main(string[] args)
        => CommandLineApplication.Execute<Program>(args);

        [Argument(0, Description = "Configuration file")]
        [FileExists]
        public string ConfigurationFile { get; }

        [Argument(1, Description = "Comparison file 1")]
        [FileExists]
        public string ComparisonFile1 { get; }

        [Argument(2, Description = "Comparison File 2")]
        [FileExists]
        public string ComparisonFile2 { get; }

        private void OnExecute()
        {
            Console.WriteLine(ConfigurationFile);
            Console.WriteLine(ComparisonFile1);
            Console.WriteLine(ComparisonFile2);
        }
    }
}

do a dotnet build Go to the dll folder that was just build most likely in Debug\netcoreapp2.2\ Create a fake file A.json this is required because the utility will check if the file exists. Run it with dotnet command dotnet TestConsolUtils.dll A.json A.json A.json There are a lot more you can do with this utill just look at the documentation.

Upvotes: 1

EylM
EylM

Reputation: 6103

Take a look at the documentation of Main function arguments

Assuming this is your main function and you want to accept 3 parameters:

static int Main(string[] args)
{
    // check the length of args for validation.
    // args[0] -> Configuration file
    // args[1] -> Comparison File 1
    // args[2] -> Comparison File 2
    ..... DO SOMETHING...
    return 0;
}

Usage (from command line or debugger):

SomeProgram.exe "ConfigFilePath" "ComparisonFile1" "ComparisonFile2".

Upvotes: 3

Related Questions