Repository: Flexget/Flexget

Issue:
--cli-config only works in test mode
### Expected behaviour:

Passing configuration parameters to `flexget execute` from the command line with `--cli-config` should work in normal (non-test) mode. The values passed through the command line should be replaced in the configuration variables the same way the are in test mode.
### Actual behaviour:

It works as expected as long as the `--test` flag is used. Without the `--test` flag variables in the configuration are not replaced by the values passed with `--cli-config`, instead, the literal variable name is used, i.e. `$variable`. 
### Steps to reproduce:
- Step 1: Using the minimal configuration below.
- Step 2: To observe correct behaviour execute in test mode: 

`flexget --test execute --tasks test --cli-config variable=foobar -v --dump`
- Step 3: To observe incorrect behaviour execute in normal mode:

`flexget execute --tasks test --cli-config variable=foobar -v --dump`
#### Config:

```
tasks:
  test:
    mock:
      - title: $variable
```
#### Log:
##### Test mode log:

The output is as expected. See how $variable is evaluated to 'foobar' in the entry title:

```
2016-07-28 10:27 INFO     manager                       Test mode, creating a copy from database ...
2016-07-28 10:27 INFO     manager                       Test database created
2016-07-28 10:27 VERBOSE  task_queue                    There are 1 tasks to execute. Shutdown will commence when they have completed.
2016-07-28 10:27 VERBOSE  details       test            Produced 1 entries.
2016-07-28 10:27 WARNING  task          test            Task does not have any filter plugins to accept entries. You need at least one to accept the entries you  want.
2016-07-28 10:27 VERBOSE  details       test            Summary - Accepted: 0 (Rejected: 0 Undecided: 1 Failed: 0)
2016-07-28 10:27 WARNING  task          test            Task doesn't have any output plugins, you should add (at least) one!
-- Undecided: --------------------------
title            : foobar
url              : mock://localhost/mock/3433925302934160649
original_url     : mock://localhost/mock/3433925302934160649
quality          : <LazyField - value will be determined when it is accessed>
task             : test

2016-07-28 10:27 VERBOSE  verbose       test            UNDECIDED: `foobar`
2016-07-28 10:27 VERBOSE  verbose       test            Undecided entries have not been accepted or rejected. If you expected these to reach output, you must set up filter plugin(s) to accept them.
2016-07-28 10:27 INFO     manager                       Removed test database
```
##### Normal mode log:

Incorrect behaviour. $variable is not evaluated in the entry title:

```
There is a FlexGet process already running for this config, sending execution there.
2016-07-28 10:32 VERBOSE  details       test            Produced 1 entries.
2016-07-28 10:32 WARNING  task          test            Task does not have any filter plugins to accept entries. You need at least one to accept the entries you  want.
2016-07-28 10:32 VERBOSE  details       test            Summary - Accepted: 0 (Rejected: 0 Undecided: 1 Failed: 0)
2016-07-28 10:32 WARNING  task          test            Task doesn't have any output plugins, you should add (at least) one!
-- Undecided: --------------------------
title            : $variable
url              : mock://localhost/mock/3088942986836962869
original_url     : mock://localhost/mock/3088942986836962869
quality          : <LazyField - value will be determined when it is accessed>
task             : test

2016-07-28 10:32 VERBOSE  verbose       test            UNDECIDED: `$variable`
2016-07-28 10:32 VERBOSE  verbose       test            Undecided entries have not been accepted or rejected. If you expected these to reach output, you must set up filter plugin(s) to accept them.
```
### Additional information:
- Flexget Version: 2.2.0
- Python Version: 2.7.6
- Installation method: pip
- OS and version: Ubuntu 14.04.4



Discussion:
hmm, i think this has more to do than daemon running than test mode. can you try without daemon running?

Yes, I confirm that it works as expected WITHOUT the daemon running. Is there any way around this?

It is a bug, just not related to test mode. It should invoke config reload when used, I'll try to fix soon. 

Thanks a lot I would appreciate it! I could also try to give it a try myself but I am not familiar with the code. ...perhaps with some guidance I could manage it.


Resolving patch:
diff --git a/flexget/manager.py b/flexget/manager.py
--- a/flexget/manager.py
+++ b/flexget/manager.py
@@ -222,6 +222,15 @@ def tasks(self):
     def has_lock(self):
         return self._has_lock
 
+    @property
+    def should_reload(self):
+        """ Add triggers to the list to trigger a config reload from memory. Needed for some options to work while
+         daemon is running """
+        reload_triggers = ['execute.cli_config']
+        if any(getattr(self.options, trigger, False) for trigger in reload_triggers):
+            return True
+        return False
+
     def execute(self, options=None, output=None, loglevel=None, priority=1):
         """
         Run all (can be limited with options) tasks from the config.
@@ -263,6 +272,9 @@ def execute(self, options=None, output=None, loglevel=None, priority=1):
         # TODO: 1.2 This is a hack to make task priorities work still, not sure if it's the best one
         task_names = sorted(task_names, key=lambda t: self.config['tasks'][t].get('priority', 65535))
 
+        # A hack to make specific option work by revalidating the config
+        if self.should_reload:
+            self.update_config(self.validate_config())
         finished_events = []
         for task_name in task_names:
             task = Task(self, task_name, options=options, output=output, loglevel=loglevel, priority=priority)
@@ -326,6 +338,7 @@ def handle_cli(self, options=None):
         """
         if not options:
             options = self.options
+        self.options = options
         command = options.cli_command
         command_options = getattr(options, command)
         # First check for built-in commands


Test patch: