Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
My C# classes will create a GUI that takes in csv files and plot a line graph accordingly (Refer to: C# Read string from CSV file and plot line graph for graph code[^]). Currently, my graphs are plotted and saved using the GUI file dialog.

What I am trying to do now is to read, plot and save the graph using command-line instead (NO GUI needed), such as "graphdemo.exe -f c:\\desktop\\1.csv (csv file path) -o c:\\desktop\\1.png (output graph file path)".

May I know how could I call my Read and Plot classes using the "-f" flag and save the plotted graph using the "-o" flag ?

Thanks.

What I have tried:

class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {           
            if (args.Length > 0)
            {
                if (args.Contains("-f")) //-f: Read user-input csv file path and plot line graph
                {
                    for (int i = 1; i < args.Length; i++)
                    {
                        string filepath = args[i]; //Store each file path into an array

                        if (File.Exists(filepath))
                        {
                            string filename = Path.GetFileName(filepath); //Get file name
                            //read csv file from Read class
                            //plot line graph from Plot class

                            Console.WriteLine(filename);
                        }
                        else
                        {
                            Console.WriteLine("\nPlease enter a valid file name. ");
                        }
                    }
                }
                if (args.Contains("-o"))    
                {
                    // -o: Save plotted graph as png image based on user-input path
                }

                else
                {
                    Console.WriteLine("\nPlease enter a valid argument. ");
                }
            }
            else 
            {
                Application.Run(new GraphDemo());   // Show graph GUI
            }
        }
    }
}
Posted
Updated 22-Jan-17 18:20pm
v4

That's not a good idea: there are quite a few problems with your code here.
Trivial stuff:
string filepath = args[i]; //Store each file path into an array
No, it doesn't. And a wrong comment is much, much worse than no comment.
The main problem is that you aren't considering what happens if the user makes a mistake: what if he types parameters in the wrong order.
While it's ok to use -f and -o, the normal default is
exename infile {outfile} {parameters}
Where parameters are marked by starting with either a '-' or a '/'.
The way I handle it is to preset:
C#
string inpath = LoadLastInputFile();
string outpath = inpath;
bool firstFile = true;
foreach (string arg in args)
   {
   if (arg.StartsWith('/') || arg.StartsWith('-'))
      {
      HandleOption(arg);
      }
   else
      {
      if (firstFile)
         {
         if (!File.Exists(arg))
             {
             Console.WriteLine("Input file not found: {0}", arg);
             return;
             }
         inpath = arg;
         outpath = inpath;
         firstFile = false;
         }
      else
         {
         outpath = arg;
         }
      }
   }
 
Share this answer
 
Comments
Javiertxl 23-Jan-17 0:19am    
How would I apply your code for my scenario? Not sure how do I call my Read and Plot classes, as well as making sure the input after "-f" are used for read & plot while "-o" are the output graph png.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900