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 6 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
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>
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ 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!


[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 modulare framework for multi-modal modeling of scientific data."""
Copy link
Contributor

Choose a reason for hiding this comment

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

modulare?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Typo must've been made when package was created, Ive fixed that everywhere its found


# package version
from diffpy.cmi.version import __version__ # noqa
Expand Down
58 changes: 58 additions & 0 deletions src/diffpy/cmi/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import getopt
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 short 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

Copy link
Contributor

Choose a reason for hiding this comment

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

is pre-commit doing this or can we close up these blank lines? Currently,

this

is a

bit too

spread out for my liking....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

precommit does a cr for the parenths but i removed the lines

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

Options:
-V, --version Show version and exit
Copy link
Contributor

Choose a reason for hiding this comment

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

why is the V capitalized? Is that some standard coming from Python? If not, let's lower-case it.

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 I capitalized because -v typically stands for verbose. I don't think it matters too much so we could change it to -v or simply remove the short-hand and require users to do --version. I feel like when I'm looking for a package version i always type --version anyways.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, let's remove the shortcut to remove confusion

-h, --help Show this message and exit
"""
)


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


def main():
try:
Copy link
Contributor

Choose a reason for hiding this comment

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

please do this work using argparse

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

opts, args = getopt.gnu_getopt(sys.argv[1:], "hV", ["help", "version"])
except getopt.GetoptError as err:
print(f"Error: {err}", file=sys.stderr)
usage()
sys.exit(1)

for opt, _ in opts:
if opt in ("-h", "--help"):
usage()
return
elif opt in ("-V", "--version"):
version()
return

# Default behavior (if no arguments)
usage()


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