Repository: spyder-ide/spyder

Issue:
Not working working directory (-w) of project (-p) from command line
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [x] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [x] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [x] Tried basic troubleshooting (if a bug/error)
    * [x] Restarted Spyder
    * [x] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [x] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description

The command line options for working directory and project do not work on spyder 4.2.0 


### What steps reproduce the problem?

1. Execute $ spyder -w some/dir
2. Be sad because it opened directory from last selected project (pwd and graphical interface)

Or:

1. Execute $ spyder -p some/dir
2. Be sad because it opened directory from last selected project (pwd and graphical interface)

### What is the expected output? What do you see instead?

I would like those command line parameter to work because it can be very usefull when using from a command line and it avoids going through the folder selection.

note that is checked the code and the command line parameters are parsed correctly, it meas that somehow the workingdirectory/projects plugin are not doing their job or that some initialisation in the app failed (could not find it I don't know the code well).

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 4.2.0 
* Python version: 3.6 and Python 3.8.5 64-bit
* Qt version: Qt 5.12.10  
* PyQt version: PyQt5 5.12.3 
* Operating System name/version: Ubuntu 18.04 and 20.04, Linux 5.4.0-56-generic 



Discussion:

Resolving patch:
diff --git a/spyder/app/mainwindow.py b/spyder/app/mainwindow.py
--- a/spyder/app/mainwindow.py
+++ b/spyder/app/mainwindow.py
@@ -1291,9 +1291,11 @@ def post_visible_setup(self):
         if self.splash is not None:
             self.splash.hide()
 
-        if (self.open_project and
-                (running_in_mac_app() and not self.first_spyder_run)):
-            self.projects.open_project(self.open_project)
+        if self.open_project:
+            if not running_in_mac_app():
+                self.projects.open_project(
+                    self.open_project, workdir=self.init_workdir
+                )
         else:
             # Load last project if a project was active when Spyder
             # was closed
diff --git a/spyder/plugins/projects/plugin.py b/spyder/plugins/projects/plugin.py
--- a/spyder/plugins/projects/plugin.py
+++ b/spyder/plugins/projects/plugin.py
@@ -285,7 +285,7 @@ def _create_project(self, path):
         self.open_project(path=path)
 
     def open_project(self, path=None, restart_consoles=True,
-                     save_previous_files=True):
+                     save_previous_files=True, workdir=None):
         """Open the project located in `path`"""
         self.unmaximize()
         if path is None:
@@ -329,7 +329,10 @@ def open_project(self, path=None, restart_consoles=True,
         self.set_option('current_project_path', self.get_active_project_path())
 
         self.setup_menu_actions()
-        self.sig_project_loaded.emit(path)
+        if workdir and osp.isdir(workdir):
+            self.sig_project_loaded.emit(workdir)
+        else:
+            self.sig_project_loaded.emit(path)
         self.sig_pythonpath_changed.emit()
         self.watcher.start(path)
 


Test patch: