Skip to content

feat: create app for diffpy.cmi CLI #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/source/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ Here is how you can write a block of code in the documentation. You can use the

# Create a new environment, without build dependencies (pure Python package)
conda create -n <package_name>-env python=3.13 \
--file requirements/test.txt \
--file requirements/tests.txt \
--file requirements/conda.txt

# Create a new environment, with build dependencies (non-pure Python package)
conda create -n <package_name>-env python=3.13 \
--file requirements/test.txt \
--file requirements/tests.txt \
--file requirements/conda.txt \
--file requirements/build.txt

Expand Down
2 changes: 1 addition & 1 deletion doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

.. |title| replace:: diffpy.cmi documentation

``diffpy.cmi`` - Complex modeling infrastructure: a modulare framework for multi-modal modeling of scientific data.
``diffpy.cmi`` - Complex modeling infrastructure: a modular framework for multi-modal modeling of scientific data.

| Software version |release|
| Last updated |today|.
Expand Down
23 changes: 23 additions & 0 deletions news/app-creation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Add CLI to return diffpy.cmi version and help page.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ authors = [
maintainers = [
{ name="Simon Billinge", email="[email protected]" },
]
description = "Complex modeling infrastructure: a modulare framework for multi-modal modeling of scientific data."
description = "Complex modeling infrastructure: a modular framework for multi-modal modeling of scientific data."
keywords = ['modelling', 'regression', 'diffraction', 'PDF', 'complex-modeling']
readme = "README.rst"
requires-python = ">=3.11, <3.14"
Expand Down Expand Up @@ -48,6 +48,10 @@ include = ["*"] # package names should match these glob patterns (["*"] by defa
exclude = [] # exclude packages matching these glob patterns (empty by default)
namespaces = false # to disable scanning PEP 420 namespaces (true by default)

[project.scripts]
diffpy-cmi = "diffpy.cmi.app:main"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we also have one that is just cmi (for lazy people). It can run the same function.

Also, what happens if you just type diffpy-cmi or cmi without -v or -h? Let's test that and make sure it behaves in a nice friendly way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbillinge just typing diffpy-cmi also returns the same thing as the -h command.

shall we also have one that is just cmi (for lazy people). It can run the same function.

Yeah we can do that!

cmi = "diffpy.cmi.app:main"

[tool.setuptools.dynamic]
dependencies = {file = ["requirements/pip.txt"]}

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/diffpy/cmi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# See LICENSE.rst for license information.
#
##############################################################################
"""Complex modeling infrastructure: a modulare framework for multi-modal modeling of scientific data."""
"""Complex modeling infrastructure:
a modular framework for multi-modal modeling of scientific data."""

# package version
from diffpy.cmi.version import __version__ # noqa
Expand Down
75 changes: 75 additions & 0 deletions src/diffpy/cmi/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import argparse
import sys

from diffpy.cmi.version import __version__


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason to use argparse is that it does most of this work for you. It will print usage and help so you don't need those functions. Something like

    parser = argparse.ArgumentParser(
        description="help/usage message"
    )
    parser.add_argument(
        "--version",
        action="store_true"
        help="Show the program's version number and exit",
    )
   args = parser.parse_args()
   if args.version:
      print("version message")
   else:
       print("help/intro message")

should do everything you want

def usage():
"""Print full help message."""
print(
"""\

Welcome to diffpy-CMI, a complex modeling infrastructure
designed for multi-modal analysis of scientific data.
While broadly applicable to a wide range of problems,
including those beyond materials science, diffpy-CMI currently
provides robust tools for modeling atomic structure based on
Pair Distribution Function (PDF) and Small-Angle Scattering (SAS) data.
Its modular Python architecture enables extensible workflows,
with additional capabilities continually being developed.

Docs: https://www.diffpy.org/diffpy.cmi

Usage:
diffpy-cmi [--version] [--help]

Options:
--version Show version and exit
-h, --help Show this message and exit
"""
)


def short_usage():
"""Print brief usage message for invalid input."""
print(
"Usage: diffpy-cmi [--version] [--help]\nUse --help to see more.",
file=sys.stderr,
)


def print_version():
print(f"diffpy-cmi {__version__}")


def main():
parser = argparse.ArgumentParser(
prog="diffpy-cmi",
add_help=False,
)
parser.add_argument(
"--version", action="store_true", help="Show version and exit"
)
parser.add_argument(
"-h", "--help", action="store_true", help="Show this message and exit"
)
args, unknown = parser.parse_known_args()
if unknown:
print(
f"Error: unrecognized arguments: {' '.join(unknown)}",
file=sys.stderr,
)
short_usage()
sys.exit(1)
if args.help:
usage()
return
if args.version:
print_version()
return
# Default behavior (no args)
usage()


if __name__ == "__main__":
main()
Loading