Skip to content
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

Add shorthand syntax in cli to specify host and build in 1 argument #14727

Merged
merged 18 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
26 changes: 20 additions & 6 deletions conan/api/subapi/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,26 @@ def get_default_build(self):
return loader.get_default_build()

def get_profiles_from_args(self, args):
build = [self.get_default_build()] if not args.profile_build else args.profile_build
host = [self.get_default_host()] if not args.profile_host else args.profile_host
profile_build = self.get_profile(profiles=build, settings=args.settings_build,
options=args.options_build, conf=args.conf_build)
profile_host = self.get_profile(profiles=host, settings=args.settings_host,
options=args.options_host, conf=args.conf_host)
all_profiles = args.profile_all or []
all_settings = args.settings_all or []
all_options = args.options_all or []
all_conf = args.conf_all or []

build_profiles = all_profiles + (args.profile_build or [self.get_default_build()])
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
build_settings = all_settings + (args.settings_build or [])
build_options = all_options + (args.options_build or [])
build_conf = all_conf + (args.conf_build or [])

host_profiles = all_profiles + (args.profile_host or [self.get_default_host()])
host_settings = all_settings + (args.settings_host or [])
host_options = all_options + (args.options_host or [])
host_conf = all_conf + (args.conf_host or [])

profile_build = self.get_profile(profiles=build_profiles, settings=build_settings,
options=build_options, conf=build_conf)

profile_host = self.get_profile(profiles=host_profiles, settings=host_settings,
options=host_options, conf=host_conf)
return profile_host, profile_build

def get_profile(self, profiles, settings=None, options=None, conf=None, cwd=None):
Expand Down
56 changes: 36 additions & 20 deletions conan/cli/args.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import argparse

from conan.cli.command import OnceArgument
from conan.errors import ConanException

Expand Down Expand Up @@ -56,45 +58,59 @@ def add_common_install_arguments(parser):


def add_profiles_args(parser):
def profile_args(machine, short_suffix="", long_suffix=""):
def profile_args(machine, short_suffix="", long_suffix="", suppress_help=False):
parser.add_argument("-pr{}".format(short_suffix),
"--profile{}".format(long_suffix),
default=None, action="append",
dest='profile_{}'.format(machine),
help='Apply the specified profile to the {} machine'.format(machine))
help="" if suppress_help else
'Apply the specified profile. '
'By default, or if specifying -pr:h (--profile:host), it applies to the host context. '
'Use -pr:b (--profile:build) to specify the build context, '
'or -pr:a (--profile:all) to specify both contexts at once')

def settings_args(machine, short_suffix="", long_suffix=""):
def settings_args(machine, short_suffix="", long_suffix="", suppress_help=False):
parser.add_argument("-s{}".format(short_suffix),
"--settings{}".format(long_suffix),
action="append",
dest='settings_{}'.format(machine),
help='Settings to build the package, overwriting the defaults'
' ({} machine). e.g.: -s{} compiler=gcc'.format(machine,
short_suffix))

def options_args(machine, short_suffix="", long_suffix=""):
help="" if suppress_help else
'Settings for the package, overwriting the defaults. '
'By default, or if specifying -s:h (--settings:host), it applies to the host context. '
'Use -s:b (--settings:build) to specify the build context, '
'or -s:a (--settings:all) to specify both contexts at once. '
'Example: -s compiler=gcc')

def options_args(machine, short_suffix="", long_suffix="", suppress_help=False):
parser.add_argument("-o{}".format(short_suffix),
"--options{}".format(long_suffix),
action="append",
dest="options_{}".format(machine),
help='Define options values ({} machine), e.g.:'
' -o{} Pkg:with_qt=true'.format(machine, short_suffix))

def conf_args(machine, short_suffix="", long_suffix=""):
help="" if suppress_help else
'Define options values. '
'By default, or if specifying -o:h (--options:host), it applies to the host context. '
'Use -o:b (--options:build) to specify the build context, '
'or -o:a (--options:all) to specify both contexts at once. '
'Example: -o pkg:with_qt=true')

def conf_args(machine, short_suffix="", long_suffix="", suppress_help=False):
parser.add_argument("-c{}".format(short_suffix),
"--conf{}".format(long_suffix),
action="append",
dest='conf_{}'.format(machine),
help='Configuration to build the package, overwriting the defaults'
' ({} machine). e.g.: -c{} '
'tools.cmake.cmaketoolchain:generator=Xcode'.format(machine,
short_suffix))
help=argparse.SUPPRESS if suppress_help else
'Configuration to build the package, overwriting the defaults'
'By default, or if specifying -c:h (--conf:host), it applies to the host context. '
'Use -c:b (--conf:build) to specify the build context, '
'or -c:a (--conf:all) to specify both contexts at once. '
'Example: -c tools.cmake.cmaketoolchain:generator=Xcode')

for item_fn in [options_args, profile_args, settings_args, conf_args]:
item_fn("host", "",
"") # By default it is the HOST, the one we are building binaries for
item_fn("build", ":b", ":build")
item_fn("host", ":h", ":host")
# By default, it is the HOST, the one we are building binaries for
item_fn("host", "", "")
item_fn("build", ":b", ":build", suppress_help=True)
item_fn("host", ":h", ":host", suppress_help=True)
item_fn("all", ":a", ":all", suppress_help=True)


def add_reference_args(parser):
Expand Down
13 changes: 13 additions & 0 deletions conans/test/integration/command/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ def test_ignore_paths_when_listing_profiles():
assert ignore_path not in c.out


def test_shorthand_syntax():
tc = TestClient()
tc.save({"profile": "[conf]\nuser.profile=True"})
tc.run("profile show -o:a=both_options=True -pr:a=profile -s:a=os=WindowsCE -s:a=os.platform=conan -c:a=user.conf.cli=True")

# All of them show up twice, once per context
assert tc.out.count("both_options=True") == 2
assert tc.out.count("os=WindowsCE") == 2
assert tc.out.count("os.platform=conan") == 2
assert tc.out.count("user.conf.cli=True") == 2
assert tc.out.count("user.profile=True") == 2


def test_profile_show_json():
c = TestClient()
c.save({"myprofilewin": "[settings]\nos=Windows",
Expand Down
6 changes: 5 additions & 1 deletion conans/test/unittests/cli/common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ def argparse_args():
return MagicMock(
profile_build=None,
profile_host=None,
profile_all=None,
settings_build=None,
settings_host=None,
settings_all=None,
options_build=None,
options_host=None,
options_all=None,
conf_build=None,
conf_host=None
conf_host=None,
conf_all=None,
)


Expand Down