AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

Command line interface

Forum to discuss Image Processing Lab (IPLab) application, its features, etc.

Command line interface

Postby IgorMilavec » Wed Dec 23, 2009 10:09 am

Hi.

What is your opinion on supporting integration options from the command line, i.e.:
/paste (automatically paste from the clipboard upon starting)
/open <filename> (automatically open the specified file upon starting)

Also, it would be nice to integrate into Windows shell and provide "Open in Image Processing Lab" context menu for image files.

If you want to include this functionality I can provide the code for command line processing.

Regards, Igor
IgorMilavec
 
Posts: 4
Joined: Wed Dec 23, 2009 10:01 am

Re: Command line interface

Postby andrew.kirillov » Wed Dec 23, 2009 3:56 pm

Hello

Yes, these features seem to be nice. If you could send an SVS patch to speed up their integration, I would appreciate.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 2886
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Command line interface

Postby IgorMilavec » Thu Dec 24, 2009 10:10 am

Hi.

Here is the patch (sorry for pasting it here, it seems that .diff and .patch attachments are not permitted):
Code: Select all
Index: MainForm.cs
===================================================================
--- MainForm.cs   (revision 39)
+++ MainForm.cs   (working copy)
@@ -26,7 +26,17 @@
   /// </summary>
   public class MainForm : System.Windows.Forms.Form, IDocumentsHost
   {
-        private static string configFile = Path.Combine( System.Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData ), "app.config" );
+        /// <summary>
+        /// Specifies the action the application performs upon starting.
+        /// </summary>
+        protected enum AutoOpenMode
+        {
+            None,
+            Clipboard,
+            File
+        }
+
+        private static string configFile = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "app.config");
         private static string dockManagerConfigFile = Path.Combine( System.Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData ), "DockManager.config" );

      private int unnamedNumber = 0;
@@ -121,8 +131,14 @@
      private System.Windows.Forms.MenuItem statisticsViewItem;
      private System.ComponentModel.IContainer components;

-      public MainForm()
+        private AutoOpenMode autoOpenMode;
+        private string autoOpenParam;
+
+        protected MainForm(AutoOpenMode autoOpenMode, string autoOpenParam)
      {
+            this.autoOpenMode = autoOpenMode;
+            this.autoOpenParam = autoOpenParam;
+
         //
         // Required for Windows Form Designer support
         //
@@ -870,9 +886,54 @@
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
-      static void Main()
+      static void Main(string[] args)
      {
-         Application.Run(new MainForm());
+            // Parse the command line
+            bool showUsage = false;
+            AutoOpenMode autoOpenMode = AutoOpenMode.None;
+            string autoOpenParam = string.Empty;
+            if (args.Length >= 1)
+            {
+                if (string.Compare(args[0], "/paste", true) == 0)
+                {
+                    autoOpenMode = AutoOpenMode.Clipboard;
+                    if (args.Length >= 2)
+                    {
+                        showUsage = true;
+                    }
+                }
+                else if (string.Compare(args[0], "/open", true) == 0)
+                {
+                    autoOpenMode = AutoOpenMode.File;
+                    if (args.Length == 2)
+                    {
+                        autoOpenParam = args[1];
+                    }
+                    else
+                    {
+                        showUsage = true;
+                    }
+                }
+                else if (File.Exists(args[0]))
+                {
+                    // Handle the case where somebody just drops the file on the application icon
+                    autoOpenMode = AutoOpenMode.File;
+                    autoOpenParam = args[0];
+                }
+                else
+                {
+                    showUsage = true;
+                }
+            }
+
+            if (showUsage)
+            {
+                MessageBox.Show("Usage:\tiplab.exe [/paste | /open <fileName>]\r\n\r\nOptions:\t/paste\tPaste the contents of the clipboard.\r\n\t/open\tOpen the specified file.\r\n", "Image Processing Lab", MessageBoxButtons.OK, MessageBoxIcon.Information);
+            }
+            else
+            {
+                Application.Run(new MainForm(autoOpenMode, autoOpenParam));
+            }
      }

      #region IDocumentsHost implementation
@@ -996,6 +1057,17 @@

         // show histogram
         ShowHistogram(config.histogramVisible);
+
+            switch (autoOpenMode)
+            {
+                case AutoOpenMode.Clipboard:
+                    PasteFromClipboard();
+                    break;
+
+                case AutoOpenMode.File:
+                    OpenFile(autoOpenParam);
+                    break;
+            }
      }

      // Callback for loading Dock Manager
@@ -1100,31 +1172,37 @@
      {
         if (ofd.ShowDialog() == DialogResult.OK)
         {
-            ImageDoc imgDoc = null;
-            
-            try
-            {
-               // create image document
-               imgDoc = new ImageDoc(ofd.FileName, (IDocumentsHost) this);
-               imgDoc.Text = Path.GetFileName(ofd.FileName);
+                OpenFile(ofd.FileName);
+         }      
+      }

-            }
-            catch (ApplicationException ex)
-            {
-               MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
-            }
+        // Open specified file
+        private void OpenFile(string fileName)
+        {
+            ImageDoc imgDoc = null;

-            if (imgDoc != null)
-            {
-               imgDoc.Show(dockManager);
-               imgDoc.Focus();
+            try
+            {
+                // create image document
+                imgDoc = new ImageDoc(fileName, (IDocumentsHost)this);
+                imgDoc.Text = Path.GetFileName(fileName);

-               // set events
-               SetupDocumentEvents(imgDoc);
-            }
-         }      
-      }
+            }
+            catch (ApplicationException ex)
+            {
+                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+            }

+            if (imgDoc != null)
+            {
+                imgDoc.Show(dockManager);
+                imgDoc.Focus();
+
+                // set events
+                SetupDocumentEvents(imgDoc);
+            }
+        }
+
      // Show/hide histogram
      private void ShowHistogram(bool show)
      {


Let me know if this is OK.

Regards, Igor
IgorMilavec
 
Posts: 4
Joined: Wed Dec 23, 2009 10:01 am

Re: Command line interface

Postby andrew.kirillov » Tue Dec 29, 2009 8:10 am

Hello,

Sorry for a bit late reply. Just was carried away by Christmas

Could you, please, rename patch file to html and submit as attachment? HTML files are allowed. I tried patching but it looks like PHPBB changed a bit spacing/tabbing, so SVN is confused and can not patch.

Thank you.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 2886
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Command line interface

Postby IgorMilavec » Sun Jan 03, 2010 5:52 pm

Hi.

No problem, I was a little bit offline, too. :)

HTML also seems to be forbidden. Tried txt, exe , ... nothing worked. On a PM I cannot attach...
I have put the diff on http://cl1p.net/MainForm_diff/ , can you download it from there?

Regards, Igor
IgorMilavec
 
Posts: 4
Joined: Wed Dec 23, 2009 10:01 am

Re: Command line interface

Postby andrew.kirillov » Mon Jan 04, 2010 10:39 am

Hello,

IgorMilavec wrote:HTML also seems to be forbidden. Tried txt, exe , ... nothing worked. On a PM I cannot attach...

Hmm ... Maybe there is something with access rights. Need to take a look.

Anyway. Merged, committed, works fine. Will be available with next release. You are now listed as contributor!

Thank you for sharing the code.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 2886
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Command line interface

Postby IgorMilavec » Mon Jan 04, 2010 10:09 pm

Heh, not much of a contribution compared to the work you have done!

Thanks! Igor
IgorMilavec
 
Posts: 4
Joined: Wed Dec 23, 2009 10:01 am




Return to IPLab