ischneider: Once #130 is merged, the test should pass again.
ischneider: Updated with the ability to extract the filter from a provided saved search in the JSON from the `--filter-json` flag. CI still expected to fail until #131 is merged
diff --git a/planet/scripts/util.py b/planet/scripts/util.py
index 421631d..21dddf5 100644
--- a/planet/scripts/util.py
+++ b/planet/scripts/util.py
@@ -79,20 +79,23 @@ def check_writable(dirpath):
 
 
 def filter_from_opts(**kw):
-    '''Build a AND filter from the provided filter_in OR kwargs defaulting to an
-    empty 'and' filter (@todo: API workaround).
+    '''Build a AND filter from the provided kwargs defaulting to an
+    empty 'and' filter (@todo: API workaround) if nothing is provided.
+
+    If the 'filter_json' argument is provided, this will be assumed to contain
+    a filter specification and will be anded with other filters. If the
+    'filter_json' is a search, the search filter value will be used.
+
     All kw values should be tuple or list
     '''
     filter_in = kw.pop('filter_json', None)
     active = and_filter_from_opts(kw)
-    no_filters = len(active['config']) == 0
-    if no_filters and not filter_in:
-        return filters.and_filter()
-    if not no_filters and filter_in:
-        raise click.ClickException(
-            'Specify filter options or provide using --filter-json, not both')
     if filter_in:
-        active = filter_in
+        filter_in = filter_in.get('filter', filter_in)
+        if len(active['config']) > 0:
+            active = filters.and_filter(active, filter_in)
+        else:
+            active = filter_in
     return active
 
 

cli download command improperly rejects --filter-json argument
To make the download experience nicer, the download command implicitly creates a permissions filter but does not combine it with the provided filter argument and exits with the message:

`Error: Specify filter options or provide using --filter-json, not both`

It seems like the proper behavior would be:

1. if the provided filter is an AND filter, add the permissions filter as a predicate
2. otherwise create a new top-level AND filter with the permissions and provided filter as predicates

diff --git a/tests/test_v1_cli.py b/tests/test_v1_cli.py
index 8b56083..838ff07 100644
--- a/tests/test_v1_cli.py
+++ b/tests/test_v1_cli.py
@@ -55,16 +55,47 @@ def test_filter(runner):
     def filt(*opts):
         return runner.invoke(main, ['data', 'filter'] + list(opts))
     assert_success(filt(), {
-        "type": "AndFilter",
-        "config": []
+        'type': 'AndFilter',
+        'config': []
     })
     assert_success(filt('--string-in', 'eff', 'a b c'), {
-        "type": "AndFilter",
-        "config": [
+        'type': 'AndFilter',
+        'config': [
             {'config': ['a', 'b', 'c'], 'field_name': 'eff',
              'type': 'StringInFilter'}
         ]
     })
+    filter_spec = {
+        'type': 'StringInFilter',
+        'config': ['a'],
+        'field_name': 'eff'
+    }
+    # if no other options, filter-json is used as is
+    assert_success(filt('--filter-json', json.dumps(filter_spec)), filter_spec)
+    # verify we extract the filter property of a search
+    assert_success(filt('--filter-json', json.dumps({
+        "filter": filter_spec
+    })), filter_spec)
+    # filters are combined - the --string-in option results in a single AND
+    # filter and this is combined with the provided filter_spec in a top-level
+    # AND filter
+    assert_success(filt('--filter-json', json.dumps(filter_spec),
+                        '--string-in', 'eff', 'b'), {
+        'config': [
+            {
+                'type': 'AndFilter',
+                'config': [
+                    {
+                        'type': 'StringInFilter',
+                        'config': ['b'],
+                        'field_name': 'eff'
+                    }
+                ]
+            },
+            filter_spec
+        ],
+        'type': 'AndFilter'
+    })
     # @todo more cases that are easier to write/maintain
 
 

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov pytest-mock pytest-xdist
apt-get update apt-get install -y gcc
3.6
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
alabaster==0.7.13
attrs==22.2.0
Babel==2.11.0
certifi==2021.5.30
charset-normalizer==2.0.12
click==8.0.4
coverage==6.2
docutils==0.17.1
execnet==1.9.0
flake8==5.0.4
idna==3.10
imagesize==1.4.1
importlib-metadata==4.2.0
iniconfig==1.1.1
Jinja2==3.0.3
MarkupSafe==2.0.1
mccabe==0.7.0
mock==5.2.0
packaging==21.3
pex==2.33.7
-e git+https://github.com/planetlabs/planet-client-python.git@49da66f16774682e377dfd79eebe0f33ea47f8e1#egg=planet
pluggy==1.0.0
py==1.11.0
pycodestyle==2.9.1
pyflakes==2.5.0
Pygments==2.14.0
pyparsing==3.1.4
pytest==7.0.1
pytest-cov==4.0.0
pytest-mock==3.6.1
pytest-xdist==3.0.2
pytz==2025.2
requests==2.27.1
requests-futures==1.0.2
requests-mock==1.12.1
snowballstemmer==2.2.0
Sphinx==4.3.2
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
tomli==1.2.3
typing_extensions==4.1.1
urllib3==1.26.20
zipp==3.6.0

name: planet-client-python
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - alabaster==0.7.13
      - attrs==22.2.0
      - babel==2.11.0
      - charset-normalizer==2.0.12
      - click==8.0.4
      - coverage==6.2
      - docutils==0.17.1
      - execnet==1.9.0
      - flake8==5.0.4
      - idna==3.10
      - imagesize==1.4.1
      - importlib-metadata==4.2.0
      - iniconfig==1.1.1
      - jinja2==3.0.3
      - markupsafe==2.0.1
      - mccabe==0.7.0
      - mock==5.2.0
      - packaging==21.3
      - pex==2.33.7
      - pluggy==1.0.0
      - py==1.11.0
      - pycodestyle==2.9.1
      - pyflakes==2.5.0
      - pygments==2.14.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-cov==4.0.0
      - pytest-mock==3.6.1
      - pytest-xdist==3.0.2
      - pytz==2025.2
      - requests==2.27.1
      - requests-futures==1.0.2
      - requests-mock==1.12.1
      - setuptools==20.10.1
      - snowballstemmer==2.2.0
      - sphinx==4.3.2
      - sphinxcontrib-applehelp==1.0.2
      - sphinxcontrib-devhelp==1.0.2
      - sphinxcontrib-htmlhelp==2.0.0
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==1.0.3
      - sphinxcontrib-serializinghtml==1.1.5
      - tomli==1.2.3
      - typing-extensions==4.1.1
      - urllib3==1.26.20
      - zipp==3.6.0
prefix: /opt/conda/envs/planet-client-python

tests/test_v1_cli.py::test_filter
tests/test_v1_cli.py::test_filter_options_invalid
tests/test_v1_cli.py::test_quick_search tests/test_v1_cli.py::test_download_errors tests/test_v1_cli.py::test_download_dry_run tests/test_v1_cli.py::test_download_quick tests/test_v1_cli.py::test_download_search_id tests/test_v1_cli.py::test_create_search tests/test_v1_cli.py::test_geom_filter
planet/scripts/util.py
planet/scripts/util.py