j-maly / CommandLineParser

Command line parser. Declarative arguments support. Rich set of argument types (switches, enums, files, etc...). Mutually exclusive arguments validations.
MIT License
137 stars 30 forks source link

Suggestion: File location default to current directory #53

Closed askids closed 6 years ago

askids commented 6 years ago

hi,

Had a question on FileArgument. When we use the FileArgument, do we always have to give the full path? With the FileMustExist condition set to true, the library will try to validate if file exists or not. In many cases, the file might be available in the same directory as the current executable for which the arguments are being passed. So I think that in such cases, it would be simpler to pick up the file from the current directory instead of always having to provide the full file path. This way, the size of the command line argument also can be shortened and can be kept less cluttered, when dealing with multiple arguments. Wherever needed, we can still continue to provide the full file path.

So similar to FileMustExist attribute, if we had similar attribute, say CheckCurDir, which the user can use to indicate if the file check must happen after considering the current directory path. So in such a case, we can first get the current execution directory and then try to see if File exists in current directory. If yes, then update the parsed argument value to full path after appending the directory. If the file is not found in current directory, then try verifying as if the given value is a full path.

Thanks!

j-maly commented 6 years ago

You don't have to use absolute paths. What happens internally is something like this

var fi = new FileInfo(path);
if (FileMustExist && !fi.Exists)
{
  throw new FileNotFoundException
}

So the path handling & validation is done by FileInfo (.NET base class library object).

So if you don't use an absolute path, it will search in the current directory of your commandline session. That might be the same directory as where your executable is or not - depends on how you call it.