Jump to content

? Conventions in CUI programs ?


Recommended Posts

Hi all,

I'm developing a console program (program.exe) that takes file A and produces file B.

What I wanted to know is what is the convention for passing files as arguments on the command line?

In other words is it convention to do:

program.exe "C:folderfile.abc"

or is it convention to use the CWD:

cd c:folder
Program.exe file.abc

Both will require different code internally. I want to know this because I am developing a program and need to know which to use.

Also, is it convention to pass command line switches before or after the main input argument?

Thanks in advance.

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Link to comment
Share on other sites

If it's a well written program, it will take a file not being able to be found as an error and tell the user that.

So, you make it so that the full path should be included if not in the same folder, and if just the file name is passed and the program can't find it, tell the user. Example, the file is is C:Tempsomefile.txt, the user starts the program from the desktop with the command line Program.exe "somefile.txt", the program can't find the file on the desktop so it's an error condition. If you make the program so that it requires the file to be in the C:Temp folder and you CD to that folder you're going to have users unhappy because they don't have a C:Temp folder.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I would add that in many of my apps that take files as commandline input (regardless if they are CUI or not), check the file with FileExists() and if true, use _PathFull() to convert whatever has been given to you to a fully qualified path. _PathFull follows the same rules as FileExists for paths, so you're guaranteed good path input to work with afterwards.

Link to comment
Share on other sites

If it's a well written program, it will take a file not being able to be found as an error and tell the user that.

So, you make it so that the full path should be included if not in the same folder, and if just the file name is passed and the program can't find it, tell the user. Example, the file is is C:Tempsomefile.txt, the user starts the program from the desktop with the command line Program.exe "somefile.txt", the program can't find the file on the desktop so it's an error condition. If you make the program so that it requires the file to be in the C:Temp folder and you CD to that folder you're going to have users unhappy because they don't have a C:Temp folder.

So basically I should require users to put their files in the current working directory, and just call program.exe file.a ?

And if it cannot be found, report error condition.

I would add that in many of my apps that take files as commandline input (regardless if they are CUI or not), check the file with FileExists() and if true, use _PathFull() to convert whatever has been given to you to a fully qualified path. _PathFull follows the same rules as FileExists for paths, so you're guaranteed good path input to work with afterwards.

This is in C++. is there a Microsoft function call equivalent?

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Link to comment
Share on other sites

PathCanonicalize in shlwapi.dll is pretty close in that it will resolve paths with . and .. However it will not prepend any root path, so if you are given a path with no root, you'll have to manually prepend the working directory first.

Edit...

PathCombine in shlwapi.dll is what you want. It will both combine two paths (a root and a relative path) and resolve relative path components . and ..

Edited by wraithdu
Link to comment
Share on other sites

So basically I should require users to put their files in the current working directory, and just call program.exe file.a ?

And if it cannot be found, report error condition.

No, what I said was that your program should take the command line parameter and see if it exists, if there's no path for the file and it's not located in the same place as your program then it doesn't exist. In cases like that you check for the file, see it's not there, tell the user that the file can't be found and to use the full path of the file or you can't process it. This way your users can put their files anywhere they want and you don't have to hard code the locations in the program.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Wouldn't _PathFull be GetFullPathNameW in kernel32.dll? Though you would have to set the working directory to the base path. How I do it in AutoIt with WinAPIEx.au3.

Func _GetFullPath($sRelativePath, $sBasePath = @WorkingDir)
    Local $sWorkingDir = @WorkingDir
    FileChangeDir($sBasePath)
    $sRelativePath = _WinAPI_GetFullPathName($sRelativePath)
    FileChangeDir($sWorkingDir)
    Return $sRelativePath
EndFunc   ;==>_GetFullPath
Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Only problem is that FileChangeDir fails if the directory doesn't exist. So you can't use the function in a 'theoretical' sense. Where _PathFull and PathCombine both work without root actually existing. The drawback to PathCombine is the MAX_PATH limit. So all options given, I use _PathFull in AutoIt. GetFullPathName would probably be the better choice in C however, especially since he's requiring the files to exist anyway.

Link to comment
Share on other sites

Only problem is that FileChangeDir fails if the directory doesn't exist. So you can't use the function in a 'theoretical' sense. Where _PathFull and PathCombine both work without root actually existing. The drawback to PathCombine is the MAX_PATH limit. So all options given, I use _PathFull in AutoIt. GetFullPathName would probably be the better choice in C however, especially since he's requiring the files to exist anyway.

Valid point wraithdu.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

twitchy, at the very least, when presented with a file on the command-line, check the current directory [GetCurrentDirectory] AND the executable directory [GetModuleFileName minus the executable]. There's also the possibility that what they entered is available in the 'App Paths' registry key, or can be found if appended to the %PATH% environment variable [PathFindOnPath]. Those extra checks are up to you though.

Btw, if you use 'App Paths', you'll need to do extra processing (using the executable name in the default value, adding paths to the environment from the 'Path' value) if you call a process using CreateProcess - or just use ShellExecute to let the O/S do that extra junk.

*edit: forgot to mention, the extra check for 'App Paths' is only for executable files

Edited by Ascend4nt
Link to comment
Share on other sites

In the long run it shouldn't matter whether or not the file is fully qualified or relatively qualified. The program should work in all situations as long as the provided path is valid.

You should also be able to mix fully qualified and relative paths within a single command line (if you take more than one file that is).

Link to comment
Share on other sites

Both will require different code internally. I want to know this because I am developing a program and need to know which to use.

No? Why would you? I'm with Richard, it should just work. That is how other console applications are doing it.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...