Repository: spyder-ide/spyder

Issue:
Implicitly create project when launched with a folder path as argument
It would be really nice if I could load Spyder from the CLI when I am in a directory, with the contents of that directory, by running `spyder .` (or `spyder /home/akdor1154/src/project`, etc). AFAICT wiith Spyder's project model, this would require Spyder to
 - check if a project exists for that directory (i guess this already happens)
 - if not, create a new one
 - open said project.

I guess an implementation of this would be quite simple, would you accept a PR to that effect?


Discussion:
Sorry but explicit is way better than implicit: I guess many users would be surprised if Spyder creates automatically a project if they made a mistake and pass a directory instead of a file to it.
imo I have been explicit though, I've passed a directory to Spyder as its argument. The only thing I could possibly want to do is open Spyder with a view of that directory. (What else might a user expect from the action of running `spyder /home/akdor1154/src/python-project` ?) Currently I am surprised because the explicit action I have taken to open this directory in Spyder appears to have no effect.
> Currently I am surprised because the explicit action I have taken to open this directory in Spyder appears to have no effect.

We simply don't open directories (as stated in `spyder --help`)
You do though, from the user's perspective - but to do so, they have to go Project -> Create Project, then Open that project, manually. This issue is a suggestion to make that workflow easier. I commonly work with tens of new small Python projects in a single day, and having to manually Create Project for all of them is a bit of a pain. In other IDEs/editors, VSCode, Sublime, I can just open directories as projects directly from the CLI, and I find this workflow very convenient. I still don't really understand what the drawback would be of allowing Spyder to be run in this way - as I asked before, would else would a user expect to happen from running `spyder /home/akdor1154/src/python-project`?
Creating a project per opened directory would also create an `.spyproject` hidden directory in each of them, which I really don't find useful

Instead, you can use the `-w/--workdir` cli option to set the working directory to the one you like. That makes the Files pane to show that directory instead of your home one, and the IPython console and editor will also use that directory by default.

```
spyder --help

-w WORKING_DIRECTORY, --workdir WORKING_DIRECTORY
                      Default working directory
```
The `-p` / `--project` option opens a project if that is what you want, and I think it creates a new project if necessary.
thanks for the suggestions guys. :)

The behaviour of -p is nearly workable for me - there are a couple of weird things which again I'd be happy to contribute fixes for if they'd be welcome.
 - `-p .` creates a project literally called `.` I guess it should be the absolute path to the directory instead? This one is probably not controversial.
 - `-p .` interprets `.` relative to my home directory, it seems that the argument to `-p` is interpreted relative to `-w`. Any chance this could be interpreted relative to where spyder is run, if -w is not specified?

> Creating a project per opened directory would also create an .spyproject hidden directory in each of them, which I really don't find useful

AFAIK both VSCode and Sublime behave like this (if I explicitly open a directory with them) and while I too have an instinctive emotional response to "leaving crap over my filesystem" it's never caused an issue for me in practice. :)
If VSCode and Sublime do the same as us, then I'd be happy a PR to add support to create a new project with `-p` if none is present in the directory passed to it.
awesome, thanks for your consideration :)

Resolving patch:
diff --git a/spyder/app/mainwindow.py b/spyder/app/mainwindow.py
--- a/spyder/app/mainwindow.py
+++ b/spyder/app/mainwindow.py
@@ -348,7 +348,10 @@ def __init__(self, options=None):
         self.profile = options.profile
         self.multithreaded = options.multithreaded
         self.new_instance = options.new_instance
-        self.open_project = options.project
+        if options.project is not None:
+            self.open_project = osp.normpath(osp.join(CWD, options.project))
+        else:
+            self.open_project = None
         self.window_title = options.window_title
 
         logger.info("Start of MainWindow constructor")
@@ -2969,10 +2972,22 @@ def open_external_file(self, fname):
         variable explorer inside Spyder.
         """
         fname = encoding.to_unicode_from_fs(fname)
-        if osp.isfile(fname):
-            self.open_file(fname, external=True)
-        elif osp.isfile(osp.join(CWD, fname)):
-            self.open_file(osp.join(CWD, fname), external=True)
+        if osp.exists(osp.join(CWD, fname)):
+            fpath = osp.join(CWD, fname)
+        elif osp.exists(fname):
+            fpath = fname
+        else:
+            return
+
+        if osp.isfile(fpath):
+            self.open_file(fpath, external=True)
+        elif osp.isdir(fpath):
+            QMessageBox.warning(
+                self, _("Error"),
+                _('To open <code>{fpath}</code> as a project with Spyder, '
+                  'please use <code>spyder -p "{fname}"</code>.')
+                .format(fpath=osp.normpath(fpath), fname=fname)
+            )
 
     # --- Path Manager
     # ------------------------------------------------------------------------


Test patch: